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
A Provocative Remark About Buy A Driving License With
Code 95 Kup prawo jazdy Z kodem 95 (heib.ru)
buy cipro no rx: ci pharm delivery – ciprofloxacin 500 mg tablet price
cost of cheap clomid pill can i buy generic clomid pills can i purchase generic clomid online
Awesome blog! Is your theme custom made or did you download it from somewhere?
A theme like yours with a few simple tweeks would really make my blog jump out.
Please let me know where you got your design. Many thanks
Hello to all, the contents existing at this site are in fact awesome for
people knowledge, well, keep up the nice work fellows.
cost generic clomid now: clomid on pharm – where to get cheap clomid price
I like it when individuals get together and share opinions.
Great blog, keep it up!
вывод из запоя на дому санкт-петербург вывод из запоя на дому санкт-петербург .
prednisone over the counter south africa PredniBest prednisone 5 mg brand name
cheap clomid: clomidonpharm – buying cheap clomid without dr prescription
Как оказалось, купить диплом кандидата наук не так уж и сложно
Интересные статьи о займах – http://worldcrisis.ru/crisis/3973650
I need to to thank you for this good read!! I definitely loved every little bit of it.
I have got you bookmarked to look at new stuff you post…
вывод из запоя цены вывод из запоя цены .
drug markets onion darknet market darknet market lists
This post has opened my eyes to the variety of options available for bathroom renovations! Explore more at Bathroom refurbishment
https://prednibest.com/# prednisone pills cost
ordering prednisone: best pharmacy prednisone – prednisone 2 mg daily
I always used to read article in news papers but now as I am a user of internet so from now I am
using net for articles, thanks to web.
prednisone cost 10mg: PredniBest – buy prednisone online without a prescription
Building a company on-line requires advantage, and an outstanding electronic marketing employer could make the entire difference Digital Marketing Agency
вывод из запоя вывод из запоя .
вывод из запоя на дому вывод из запоя на дому .
https://prednibest.com/# generic prednisone cost
вывод из запоя капельница вывод из запоя капельница .
dark net dark internet dark web sites
Its like you read my mind! You appear to know a lot about this, like you wrote the book in it or
something. I think that you could do with some pics to drive the
message home a little bit, but instead of that, this is fantastic blog.
A fantastic read. I’ll definitely be back.
Soft washing actually helps maintain the visual appeals of your property without damage– fantastic topic covered here; discover more at http://riverpnxd748.cavandoragh.org/what-is-soft-washing-and-how-does-it-differ-from-pressure-washing
вывод из запоя санкт-петербург вывод из запоя санкт-петербург .