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
buy cipro cheap ciprofloxacin order online п»їcipro generic
Сколько стоит получить диплом высшего и среднего образования легально?
Покупка диплома о среднем полном образовании: как избежать мошенничества?
Всё, что нужно знать о покупке аттестата о среднем образовании без рисков
https://clomidonpharm.com/# order cheap clomid no prescription
buy cheap amoxicillin Amox Star amoxicillin buy no prescription
Интересные статьи о займах – https://tnv.ru/news/economy/224210-vygodnye-mfo-dlya-predprinimateley/
Unquestionably believe that which you said. Your favorite justification appeared to be on the internet
the easiest thing to be aware of. I say to you, I certainly get annoyed while people think about worries that they plainly don’t know about.
You managed to hit the nail upon the top and also defined out the
whole thing without having side-effects , people can take a signal.
Will likely be back to get more. Thanks
Medication prescribing information. Effects of Drug Abuse.
where to get reglan for sale
All about medicine. Get information here.
I’m not sure why but this website is loading extremely slow for
me. Is anyone else having this issue or is it a problem on my end?
I’ll check back later and see if the problem still exists.
Узнайте, как приобрести диплом о высшем образовании без рисков
Traveling should be frustrating, but having a journey agent simplifies all the things! Learn about their reward at travel agency myrtle beach
where can i buy cheap clomid without prescription: generic clomid tablets – where buy clomid
https://amoxstar.com/# amoxicillin 500 mg cost
вывод из запоя на дому спб цены вывод из запоя на дому спб цены .
Medicine prescribing information. Short-Term Effects.
can i order generic diflucan price
Everything about drug. Read information here.
free dark web darkmarket link darknet markets
вывод из запоя на дому санкт-петербург вывод из запоя на дому санкт-петербург .
how to buy amoxycillin: amoxil generic – amoxicillin 500 mg tablet
how can i get clomid without rx can i get generic clomid without dr prescription how to get generic clomid without insurance
The Best Folding Treadmill UK Tricks To Transform Your
Life best Folding treadmill
where to buy amoxicillin cost of amoxicillin prescription for amoxicillin
https://www.europneus.es/talleres/arcls/?el_codigo_promocional-1xbet.html
I must thank you for the efforts you have put in penning this blog.
I’m hoping to check out the same high-grade blog posts from you
later on as well. In truth, your creative writing abilities has
inspired me to get my own, personal site now 😉
A friend highly recommended seeing a chiropractor after her accident Whiplash chiropractor near me
вывод из запоя на дому вывод из запоя на дому .
Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.
I’ve been looking for a plug-in like this for quite some time and
was hoping maybe you would have some experience with something like this.
Please let me know if you run into anything. I truly enjoy
reading your blog and I look forward to your
new updates.
Как приобрести диплом техникума с минимальными рисками
This website was… how do you say it? Relevant!! Finally I have
found something which helped me. Cheers!
Интересные статьи о займах – http://rus-linux.net/press-reliz/2021-12/sravni.html