JS 依照元素出現數量排序
同樣是最近遇到的狀況,需要利用陣列內元素出現的數量進行排序,以下為範例程式:
var array = ["1", "2", "4", "5", "2", "4", "2", "3"]; var str = array.toString(); var filter = []; var result = []; document.write("原始陣列:" + array + "<br>"); var get = function (str, tar, arr, tmp) { if (str.indexOf(tar) >= 0) { tmp = str.slice(str.indexOf(tar) + 1); arr.push(tar); get(tmp, tar, arr, tmp); } return arr; } for (i in array) { var elm = []; var tmp; var fstr = filter.join(); if (fstr.indexOf(array[i]) >= 0) { continue; } else { var tmp_arr = get(str, array[i], elm, tmp); result.push(tmp_arr.length + ':' + tmp_arr[0]); filter.push(array[i]); } } result.sort(); result.reverse(); //按照出現次數排序,不刪除重複元素 var finishArray = []; for (index in result) { var show = result[index].split(':'); for (i = 0; i < show[0]; i++) { finishArray.push(show[1]); } } document.write("按出現次數排序,不刪除重複元素:" + finishArray + "<br>"); //按照出現次數排序,刪除重複元素 var finishArray = []; for (index in result) { var show = result[index].split(':'); finishArray.push(show[1]); } document.write("按出現次數排序,刪除重複元素:" + finishArray + "<br>");
輸出結果:
原始陣列:1,2,4,5,2,4,2,3 按出現次數排序,不刪除重複元素:2,2,2,4,4,5,3,1 按出現次數排序,刪除重複元素:2,4,5,3,1
Whoa a good deal of terrific advice!
Ten Upvc Window Repairs That Really Change Your Life Window Repairs
Nine Things That Your Parent Teach You About Double
Glazing Repair Near Me Double Glazing Repair Near Me
Рубероид РКК – это высококачественный материал, отвечающий всем требованиям госта. Он имеет высокую прочность, вес и габариты, а также теплостойкость до 80 градусов Рубероид РПП 300 С2
I’m truly enjoying the design and layout of your
website. It’s a very easy on the eyes which makes it much more enjoyable
for me to come here and visit more often. Did you hire out a designer to create your theme?
Fantastic work!
https://cytotec.club/# Cytotec 200mcg price
5 Killer Quora Answers To Wall Mounted Electric Fires wall
mounted electric Fires (promarket.in.Ua)
pct nolvadex: tamoxifen warning – aromatase inhibitor tamoxifen
cytotec pills buy online cytotec pills buy online buy cytotec pills online cheap
5 Killer Quora Answers To Female Pornstars Female Pornstars
online pharmacy without prescription
The Most Popular Online Shopping Figures Uk The Gurus Have Been Doing 3
Things Stand Up Paddle Board Bundle
buy cytotec online fast delivery: Misoprostol 200 mg buy online – buy misoprostol over the counter
The Reasons Defra Log Burner Is Everywhere This Year defra approved stoves – http://extension.unimagdalena.edu.co/extension/lists/contactenos/dispform.aspx?id=1208338 –
10 Websites To Help You To Become An Expert In Double Glazing Firms Near Me upvc double Glazing windows
Could Auto Accident Lawyers Be The Key For 2023’s Challenges?
auto injury lawyer near me
ciprofloxacin cipro ciprofloxacin cipro 500mg best prices
http://finasteride.store/# cheap propecia for sale
does tamoxifen cause weight loss: natural alternatives to tamoxifen – tamoxifen for sale
This Is The History Of Double Glazed Window Repair In 10 Milestones window Repairs
near me (https://www.dermandar.com)
I just like the valuable information you supply in your articles.
I will bookmark your weblog and test again right
here regularly. I am reasonably certain I will be informed many new stuff right here!
Best of luck for the next!
Speak “Yes” To These 5 Mazda 2 Key Replacement Tips
mazda key Fobs
A Look Into The Future How Will The Treadmills Industry Look Like In 10
Years? treadmills folding
prescription drug lisinopril: cost for 2 mg lisinopril – zestril drug
How Under Counter Fridge Has Become The Most Sought-After Trend Of 2023 fridge freshener
(Chris)
20 Things You Should Be Educated About Door Fitters Leeds upvc hinge repair leeds
Guide To Multi Fuel Boiler Stove: The Intermediate Guide In Multi Fuel Boiler Stove multi fuel boiler stove
The Most Hilarious Complaints We’ve Seen About Tommy Hilfiger Xbody Bag cheaper
lisinopril prescription coupon lisinopril with out prescription cost of lisinopril 20 mg
How London Online Clothing Shopping Sites Became The Hottest Trend In 2023 Outdoor Survival Axe