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
5 Asbestos Mesothelioma Attorney Lessons From Professionals Mesothelioma Lawsuits
This is really interesting, You are a very skilled blogger.
I have joined your rss feed and look forward to seeking more of your magnificent post.
Also, I’ve shared your site in my social networks!
10 Reasons That People Are Hateful To Folding Treadmills UK Folding Treadmills UK
treadmill that folds up
See What Lightest 3 Wheel Stroller Tricks The Celebs Are Utilizing lightest 3 wheel stroller
https://casinositeleri.win/# guvenilir casino siteleri
deneme bonusu veren siteler yeni
Всё, что нужно знать о покупке аттестата о среднем образовании
дипломы цена дипломы цена .
Wonderful post however , I was wanting to know if you could write a
litte more on this topic? I’d be very thankful if
you could elaborate a little bit more. Appreciate it!
https://ranitidinex.com/
Возможно ли купить диплом стоматолога, и как это происходит
This site certainly has all the info I wanted concerning this subject and didn’t know who to
ask.
https://w8.livedrawlaos.life/
deneme bonusu veren siteler mycbet.com: deneme bonusu veren siteler yerliarama.org – deneme bonusu veren siteler 2024
matadorbet bid: matadorbet bid – matadorbet
Всё, что нужно знать о покупке аттестата о среднем образовании без рисков
Тут можно преобрести огнестойкие сейфы купить сейф огнестойкий
What’s The Current Job Market For Best Self-Propelled Wheelchair Professionals?
best Self-Propelled wheelchair
slot tr online en kazancl? slot oyunlar? slot oyunlar?
matadorbet matadorbet giris matadorbet
10 Meetups On Upvc Window Replacement Hinges You Should
Attend Window Hinge repair Cost
Как избежать рисков при покупке диплома колледжа или ПТУ в России
купить диплом в куйбышеве купить диплом в куйбышеве .
Hello friends, pleasant article and good arguments commented at this place, I am genuinely enjoying by
these.
https://w2.livetogelhk.top/
Canl? Casino Siteleri: Deneme Bonusu Veren Siteler – Deneme Bonusu Veren Siteler
Five Killer Quora Answers To Mens Masturbators Mens masturbators
Всё, что нужно знать о покупке аттестата о среднем образовании
You have made some really good points there. I checked on the web for more info about the issue and found most people will go along with your views on this site.
10 Essentials Regarding Private Diagnosis ADHD
You Didn’t Learn At School adhd assessment for Adults private
Spot on with this write-up, I actually think this website needs far
more attention. I’ll probably be returning to see more, thanks for the info!
https://w7.datahk.world/
Купить диплом о среднем образовании в Москве и любом другом городе
Hello There. I found your blog using msn. This is a
very well written article. I will make sure to bookmark it and return to read more of your useful information. Thanks for the post.
I will certainly return.
mohajer-co.com
Good day I am so delighted I found your blog, I really found
you by mistake, while I was researching on Askjeeve for something else, Nonetheless I am here now and would just like to say many thanks for a tremendous post and a all round interesting blog (I also love the
theme/design), I don’t have time to browse it
all at the moment but I have bookmarked it and
also added in your RSS feeds, so when I have time I will be
back to read a great deal more, Please do keep up the great work.