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
It’s perfect time to make some plans for the long run and it is
time to be happy. I have read this post and if I may I desire
to suggest you some fascinating things or suggestions.
Perhaps you could write next articles regarding this article.
I desire to read even more things approximately it!
For any individual uncertain concerning just how to take care of big amounts of waste, leasing a dumpster is the method to go! Have a look at dumpster rental near Oak Ridge
pinup-kazi.ru: пин ап казино – pinup
пин ап вход: пинап казино – пинап казино
https://vavada-kazi.ru/# vavada-kazi.ru
hello!,I love your writing very much! proportion we communicate extra about your article on AOL?
I require an expert in this area to unravel my problem.
May be that is you! Having a look forward to look you.
Meds information sheet. Cautions.
buy generic singulair without insurance
Everything about medicament. Get information here.
My brother suggested I might like this blog. He was entirely right.
This post truly made my day. You can not imagine just how much time I had
spent for this information! Thanks!
пинап казино: пин ап кз – пин ап казино онлайн
Strong arguments here! Purchasing security can lead to long-lasting expense savings and comfort TreeStone Security Services
pinup kazi: пин ап казино официальный сайт – pinup
Hi there, I enjoy reading through your post. I like to
write a little comment to support you.
пин ап вход: pinup – пин ап зеркало
pinup: pinup-kazi.kz – пин ап казино онлайн
казино вавада: вавада казино онлайн – vavada
It’s actually a great and helpful piece of information.
I am glad that you shared this useful information with us.
Please stay us up to date like this. Thanks for sharing.
deep web markets darknet websites tor markets links
10 Healthy Habits For Replace Volvo Key volvo Xc60 replacement Key
I blog quite often and I seriously thank you for your information. The article has truly peaked my interest.
I am going to book mark your website and keep checking for new details about once
a week. I opted in for your Feed as well.
Medicine information leaflet. Brand names.
how to make codeine promethazine syrup
All trends of drugs. Read information here.
пин ап вход: pinup – пин ап зеркало
Does your blog have a contact page? I’m having problems locating it but, I’d like to send you an email.
I’ve got some suggestions for your blog you might be interested in hearing.
Either way, great site and I look forward to seeing it develop over time.
http://w4.prediksiangkakeramat.sbs/
Here is my web page: Daycares popular listings – Simone,
Официальная покупка диплома вуза с упрощенной программой обучения
I will right away take hold of your rss feed as I can not find
your email subscription link or e-newsletter service.
Do you’ve any? Kindly let me understand in order that I could subscribe.
Thanks.
вавада онлайн казино вавада казино онлайн vavada
When I originally left a comment I seem to have clicked
the -Notify me when new comments are added- checkbox and now every
time a comment is added I receive 4 emails with the exact same comment.
Perhaps there is an easy method you are able to remove me from that service?
Cheers!
I visited various websites but the audio feature for audio songs current at
this web page is truly superb.
https://caramenggugurkankandunganherbal.com/
Your means of telling all in this paragraph is actually fastidious,
all be able to effortlessly be aware of it, Thanks a lot.
my web page – timbangan Semarang (https://sapiterbang.weebly.com)
pinup: pinup-kazi.ru – пин ап казино официальный сайт