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
Excellent website. Lots of helpful info here.
I’m sending it to some friends ans also sharing in delicious.
And naturally, thanks for your effort!
нарколог краснодар http://www.zavitai.mybb.social/viewtopic.php?id=89/ .
нарколог на дом цены http://www.bisound.com/forum/showthread.php?p=1217304#post1217304/ .
prednisone cost canada: generic Prednisone – prednisone 20 mg tablets
Покупка диплома о среднем полном образовании: как избежать мошенничества?
Как избежать рисков при покупке диплома колледжа или ПТУ в России
электронный карниз для штор электронный карниз для штор .
Your way of explaining all in this piece of writing is truly good,
every one be able to easily know it, Thanks a lot https://fenestrationdessommets.com/nos-realisations/
For hottest news you have to visit internet and on web I found
this web page as a finest site for hottest updates.
нарколог на дом срочно http://www.dubna.myqip.ru/?1-5-0-00000282-000-0-0-1730730082 .
Всё, что нужно знать о покупке аттестата о среднем образовании
can you buy amoxicillin over the counter in canada: amoxil com pharm – can i buy amoxicillin over the counter in australia
Pretty nice post. I just stumbled upon your blog and wished to say that
I’ve really enjoyed browsing your blog posts. In any case
I’ll be subscribing to your feed and I hope you write again soon!
Heya i am for the first time here. I came
across this board and I find It really useful & it helped me out a lot.
I hope to give something back and help others like you aided me.
20 Myths About Adhd Assessment Adult: Busted should i get assessed for adhd
[Jerri]
clomid generic: clomid rex pharm – can i buy generic clomid no prescription
BBgate MarketPlace 2024 Breaking Bad Gate Forum
BBgate MarketPlace
You completed certain reliable points there. I did a search on the subject and found nearly all persons will agree with your blog.
Feel free to surf to my blog post … christian online
dating sites (Williams)
max pharm: max pharm – max pharm
prednisone tablets india: cheap prednisone – buy prednisone with paypal canada
ADHD Assessment Private Tools To Improve Your Daily Life ADHD Assessment Private Trick That Every Person Must Know Adhd Assessment private
dark web market darknet markets https://darknetmarketstore.com/ – deep web search
Приветствую. Может кто знает, где найтиполезные статьи о недвижимости? Сейчас читаю – https://glavsnab-gbi.ru
Приветствую. Может кто знает, где почитатьполезные статьи о недвижимости? Сейчас читаю – https://glavsnab-gbi.ru
нарколог на дом круглосуточно http://setter.borda.ru/?1-7-0-00000673-000-0-0-1730729894/ .
Individualize your pitch to resolve each potential client’s certain requirements.
Feel free to surf to my webpage … objectstorage.ap-melbourne-1.oraclecloud.com
amoxicillin 500mg capsule cost: cheap amoxil – generic amoxicillin over the counter
вызвать нарколога на дом spilkuvannya.rolevaya.com/viewtopic.php?id=66 .
Drugs information leaflet. Cautions.
how to get cheap allegra without insurance
Best information about medication. Read information here.