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
5 Killer Quora Answers On Double Glazed Window Repairs Near Me Double glazed Window repairs Near me
10 Things We Are Hateful About American Fridges Freezers narrow american fridge freezer
What’s The Reason Nobody Is Interested In L Couches For Sale sectional couch
bed – Rvolchansk.ru,
14 Cartoons About Automotive Locksmiths That’ll
Brighten Your Day automotive locksmiths near Me
вулкан россия
See What Replacement Sash Windows Tricks The Celebs Are Using replacement sash windows
11 “Faux Pas” That Are Actually OK To Make With Your Door
Fitting Leeds window hinge repair leeds (https://bookmarkfeeds.Stream/)
Five People You Must Know In The Designer Handbags For Sale Industry designer handbags
women’s (Lyle)
This Week’s Best Stories About Double.Glazing Near Me double glazing near
me (Flor)
14 Smart Strategies To Spend The Remaining Window Repairs Leeds Budget Upvc hinge repair leeds
Great article.
The Reason Everyone Is Talking About Volvo Digger Key Today
Volvo S40 Key Fob Not Working
What Is The Secret Life Of Best CSGO Case To Open Danger zone case
Regards, I like this!
15 Reasons Not To Be Ignoring Michael Kors Bags Pink michael Kors bags Sling
15 Reasons To Not Ignore Double Glazed Near Me double glazing upvc windows
Excellent blog here! Also your site loads up fast! What web host are
you using? Can I get your affiliate link to your host?
I wish my website loaded up as quickly as yours lol
партнерствуем с ведущими нотариусами Москвы, что обеспечивает лучшие цены на заверение переводов.
Have a look at my web page – https://public.tableau.com/app/profile/kursiv.ua/vizzes
order famciclovir generic – valcivir brand valaciclovir 1000mg canada
5 Killer Quora Answers To Sash Window Repair window repair
5 Must-Know Practices For Double Glazing Door Repairs Near Me In 2023 double glazing Door
10 Things You’ve Learned In Kindergarden That’ll Help You With Free Spin Slots newest slots – shinhwaspodium.com –
10 Quick Tips About Buy Mobility Scooters Near Me best electric scooter to buy
You’ll Be Unable To Guess Double Glaze Repair Near Me’s Benefits
double glaze repair near me
9 Things Your Parents Teach You About Railroad Injuries Lawsuit railroad Injuries Lawsuit
online shopping pharmacy india https://indiaph24.store/# top online pharmacy india
п»їlegitimate online pharmacies india
Ten Things You Learned At Preschool To Help You Get A Handle On Car Locks Smith Locksmith for car
5 Must-Know-Practices Of Sell Perfume From Home For 2023
sell
5 Clarifications On Mobile Car Locksmith Local Locksmiths For Cars
A Guide To Double Glazing Leeds From Beginning To End upvc door repairs roundhay