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
Приветствую. Может кто знает, где найтиразные блоги о недвижимости? Пока нашел – https://unnaty.ru
17 Signs To Know You Work With Fireplace Wall Mount wall fire place (http://www.google.com.gi)
Приобретение диплома ПТУ с сокращенной программой обучения в Москве
15 Presents For The Spare Van Keys Lover In Your Life How Much For Spare Car Key
How To Make An Amazing Instagram Video About Shop Anal Toy best male anal
toys (Reuben)
Meds information. Short-Term Effects.
can i purchase generic clomid no prescription
All information about medicine. Read here.
Всем привет! Может кто знает, где найтиполезные статьи о недвижимости? Пока нашел – https://unnaty.ru
amoxicillin 500mg capsule: order amoxicillin online – buying amoxicillin online
Как получить диплом стоматолога быстро и официально
Many cell caps simply screw in position, so you Can AGM golf cart batteries be restored (http://pirooztak.ir/)
eliminate them in this way.
Hi there, You’ve done an excellent job. I will certainly
digg it and personally suggest to my friends. I am sure they will be benefited from this site.
I am sure this paragraph has touched all the internet visitors, its really really good article on building up new blog.
Официальная покупка диплома вуза с сокращенной программой обучения в Москве
bitcoin dark web dark markets 2024 tor market links
I would like to thank you for the efforts you’ve put in penning this
blog. I am hoping to see the same high-grade content by you
in the future as well. In fact, your creative writing abilities has
inspired me to get my very own blog now 😉
Really when someone doesn’t know after that its up to
other users that they will assist, so here it occurs.
Meds information sheet. Effects of Drug Abuse.
is benicar the same as lisinopril
Best news about meds. Read now.
The 10 Scariest Things About Car Keys Spare car key copy near Me
generic plavix: Clopidogrel Best Prices – clopidogrel bisulfate 75 mg
Wow, incredible blog layout! How long have you been blogging for?
you make blogging look easy. The overall look of your site is fantastic, as well as the content!
order cytotec online: buy cytotec online – buy cytotec in usa
Сколько стоит диплом высшего и среднего образования и как его получить?
minocycline for rosacea: buy Stromectol – stromectol tablet 3 mg
вывод из запоя в стационаре анонимно https://zal.rolevaya.info/viewtopic.php?id=5384 .
https://plavixclo.com/# Cost of Plavix on Medicare
кухни москва — Стильные и функциональные кухни для московских квартир.
I’ve been surfing online more than 3 hours today, yet I never found any interesting article like yours.
It’s pretty worth enough for me. In my opinion, if all
webmasters and bloggers made good content as you did, the web will be
much more useful than ever before.
Hey There. I discovered your blog the use of
msn. That is a really well written article. I’ll make
sure to bookmark it and come back to read more
of your helpful info. Thanks for the post. I’ll certainly
return.
Asking questions are actually good thing if you are not understanding anything fully, however
this piece of writing gives fastidious understanding even.
вывод из запоя в стационаре вывод из запоя в стационаре .