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
buying prescription drugs in mexico online: mexican online pharmacy – best online pharmacies in mexico
buying prescription drugs in mexico online: mexican online pharmacy – buying from online mexican pharmacy
отремонтировать стиральную машину в москве отремонтировать стиральную машину в москве .
ремонт стиральных машин адреса ремонт стиральных машин адреса .
where to get doxycycline without dr prescription
Приветствую. Подскажите, где почитать разные блоги о недвижимости? Пока нашел https://teplohod-denisdavidov.ru
10 Failing Answers To Common U Shaped Couch
Questions: Do You Know The Right Ones? Gray u shaped Sectional
buying from online mexican pharmacy
https://cmqpharma.com/# buying prescription drugs in mexico online
mexican mail order pharmacies
mexican rx online
http://cmqpharma.com/# medicine in mexico pharmacies
mexican pharmaceuticals online
order cheap tetracycline tablets
how can i get celexa no prescription
Responsible For An Fold Flat Treadmill Budget? 12 Top
Notch Ways To Spend Your Money Zack Foxworth
medication from mexico pharmacy: mexican online pharmacy – mexican drugstore online
It’s enormous that you are getting thoughts from this paragraph as well as
from our dialogue made at this place.
I am regular reader, how are you everybody? This piece of writing posted
at this web site is actually good.
Also visit my site :: asbestos abatement ottawa
Link exchange is nothing else however it is only placing the other person’s weblog link on your
page at proper place and other person will also do
same in favor of you.
best online pharmacies in mexico
https://cmqpharma.online/# mexico drug stores pharmacies
mexican rx online
See What Upvc Window Repair Near Me Tricks The Celebs Are Making Use Of Window Repair near me
medication from mexico pharmacy: cmq pharma – best online pharmacies in mexico
Всем привет! Подскажите, где почитать полезные статьи о недвижимости? Сейчас читаю https://teplohod-denisdavidov.ru
5. Windows And Doors Leeds Projects For Any Budget door repair Leeds
buying prescription drugs in mexico online: mexico pharmacy – mexico drug stores pharmacies
mexican pharmaceuticals online: mexican pharmacy online – mexican drugstore online
Latest GTA game news https://gta-uzbek.com, tournaments, guides and strategies. Stay tuned for the best GTA gaming experience
mexican online pharmacies prescription drugs: cmq pharma – best online pharmacies in mexico
medicine in mexico pharmacies: mexican pharmacy – п»їbest mexican online pharmacies
Latest news from the world of boxing https://boks-uz.com, achievements of Resul Abbasov, Tyson Fury’s fights and much more. Everything Boxing Ambassador has.
Buzzwords De-Buzzed: 10 Different Ways For Saying Spare Car Key
Price http://www.99811760.xyz
Sports news https://gta-uzbek.com the most respected sports site in Uzbekistan, which contains the latest sports news, forecasts and analysis.
Discover the wonderful world of online games https://onlayn-oyinlar.com with GameHub. Get the latest news, reviews and tips for your favorite games. Join our gaming community today!