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
Very shortly this website will be famous among all blogging and site-building visitors, due to it’s good articles or reviews
This Week’s Best Stories Concerning American Style Fridge
Freezers American Fridges
Five Killer Quora Answers On Emergency Car Locksmith emergency car locksmith, Kate,
The Best Slot Developers Tricks To Rewrite
Your Life best slot developers – http://www.plantsg.com.sg,
https://canadaph24.pro/# canadian pharmacy
You’ll Never Guess This Cost Of Replacement Windows Uk’s Tricks Replacement windows
buy medicines online in india buy medicines from India india online pharmacy
20 Tools That Will Make You More Efficient With Citroen Ds3 Replacement Key Cost citroen C1 key fob (https://willysforsale.com/author/rootshrimp49)
reputable indian pharmacies buy medicines from India Online medicine home delivery
https://canadaph24.pro/# pharmacy rx world canada
Great article, exactly what I wanted to find.
What’s Holding Back From The Standing Desk Treadmill Industry?
treadmills desk (Avery)
https://mexicoph24.life/# medication from mexico pharmacy
https://canadaph24.pro/# canadian pharmacy uk delivery
A Trip Back In Time: How People Talked About Marc Jacobs Handbags 20 Years Ago women
20 Misconceptions About Mesothelioma Law: Busted Mesothelioma Compensation
http://mexicoph24.life/# mexico drug stores pharmacies
http://canadaph24.pro/# canadian pharmacy no rx needed
canadian pharmacy king canadian family pharmacy canadian mail order pharmacy
Online medicine home delivery: buy medicines from India – indian pharmacies safe
Clinic For Him New York
45 W 34th Ⴝt.,
New York, NY 10001, United States
+19179822246
is shockwave therapy for ed covered by insurance
mexican mail order pharmacies: mexico pharmacy – п»їbest mexican online pharmacies
https://mexicoph24.life/# best online pharmacies in mexico
See What Glass Repair Leeds Tricks The Celebs Are Making Use Of Repair
Hi there very nice blog!! Man .. Excellent .. Amazing ..
I will bookmark your web site and take the feeds additionally?
I am happy to find so many helpful information here within the submit, we want work out more
techniques in this regard, thank you for sharing. .
. . . .
mexico pharmacy cheapest mexico drugs mexico drug stores pharmacies
See What 2nd Hand Mobility Scooters For Sale Tricks The Celebs Are Making Use Of 2nd hand Mobility scooters for sale
(minecraftcommand.science)
An outstanding share! I’ve just forwarded this onto a friend who was conducting a little research on this.
And he actually ordered me breakfast due to the fact that I discovered it for him…
lol. So let me reword this…. Thank YOU for the meal!!
But yeah, thanks for spending some time to talk
about this topic here on your web page.
buying prescription drugs in mexico online: Mexican Pharmacy Online – mexico drug stores pharmacies
https://canadaph24.pro/# cross border pharmacy canada