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
http://canadaph24.pro/# certified canadian pharmacy
15 Gifts For The Leeds Window Repair Lover In Your Life
patio door repair leeds (king-Bookmark.stream)
Awesome issues here. I’m very satisfied to see your post.
Thanks so much and I’m looking forward to
contact you. Will you kindly drop me a e-mail?
Here is my web-site Pinoy SEO
https://mexicoph24.life/# mexican online pharmacies prescription drugs
30 Inspirational Quotes On Heatpump Dryer Heatpump Dryer
п»їbest mexican online pharmacies: mexican rx online – mexican pharmacy
The 12 Worst Types Online Psychiatric Assessment The Twitter Accounts That You Follow Urgent psychiatric Assessment
https://indiaph24.store/# world pharmacy india
14 Businesses Doing A Great Job At Washer And Dryer Combination washing machines on amazon
The Reason Mesothelioma Attorneys Is Everyone’s Obsession In 2023 asbestos Law
Drugs information leaflet. What side effects?
finpecia pill
Actual news about medicine. Read here.
You’ll Be Unable To Guess Adhd Symptoms In Women’s Tricks adhd symptoms
2nd Hand Mobility Scooters For Sale Tips From The Top In The Industry travel mobility scooters sale
Why Do So Many People Want To Know About New Slots Online?
reputable Slots
When I originally commented I seem to have clicked on the -Notify
me when new comments are added- checkbox and from now on every time a comment is added I
recieve 4 emails with the same comment. Perhaps there
is a way you can remove me from that service? Kudos!
10 Things That Your Competitors Inform You About Double Glazed Units Near Me repairing double Glazing Windows
https://mexicoph24.life/# mexico drug stores pharmacies
20 Tools That Will Make You More Effective
At 9kg Washing Machine Offer Washing Machine 9Kg Uk
5 Laws Anybody Working In Leeds Windows And Doors Should Be Aware Of Double glazed doors repairs
Situs Sabung Ayam Digmaan
What’s The Job Market For Best Kids Bunkbeds Professionals Like?
best kids bunkbeds (Arlie)
mexican rx online: Online Pharmacies in Mexico – best online pharmacies in mexico
synthroid 150 mg coupon
pharmacy website india Generic Medicine India to USA indian pharmacy online
7 Things You’ve Never Known About Side By Side Fridge Freezer Integrated standard
size for side by side refrigerator (https://walther-byers.technetbloggers.de)
This Is How Licensed Slots Will Look Like In 10 Years Time themed slots
Solutions To The Problems Of Three Wheel Electric Scooters For Adults For Sale three wheel scooters for sale
How To Make An Amazing Instagram Video About Mesothelioma Law asbestos
canadian pharmacy in canada
10 Top Mobile Apps For Lost Keys Car lost Keys to my Car