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
Nice weblog right here! Also your site rather a
lot up very fast! What host are you the usage of? Can I am getting
your associate link for your host? I desire my website loaded up
as quickly as yours lol
slot kumar siteleri: 2024 en iyi slot siteleri – oyun siteleri slot
canl? slot siteleri: deneme bonusu veren slot siteleri – slot bahis siteleri
Hello! I’m at work browsing your blog from my new iphone 3gs!
Just wanted to say I love reading through your blog and look forward to all your
posts! Carry on the outstanding work!
my web-site tonic greens reddit
It’s very easy to find out any topic on net as compared
to books, as I found this paragraph at this website.
https://crystal-flamingo-dc4vls.mystrikingly.com/blog/9f4d91fe0c3
casino slot siteleri: slot oyun siteleri – bonus veren casino slot siteleri
One popular urban legend states that Walt Disney had his body cryogenically frozen after death, so that future technology could bring him back to life.
My spouse and I stumbled over here coming from a different
website and thought I might check things out. I like what I see so i am just following you.
Look forward to checking out your web page yet again.
Whoa all kinds of useful data.
Wow, fantastic blog structure! How long have you been blogging for?
you make running a blog glance easy. The overall
look of your website is excellent, as neatly as the content material!
https://sweetbonanza.network/# sweet bonanza yasal site
en guvenilir slot siteleri: slot bahis siteleri – slot oyun siteleri
A motivating discussion is worth comment. There’s no doubt that that
you need to publish more about this issue, it might not be
a taboo matter but usually folks don’t discuss such issues.
To the next! All the best!!
https://sweetbonanza.network/# sweet bonanza taktik
Thank you for another informative site. Where else may just I get that kind of information written in such a perfect method?
I have a mission that I am simply now working on, and I’ve been at the look out for such information.
You have a glimpse of the future.
https://denemebonusuverensiteler.win/# deneme bonusu veren siteler
In fact no matter if someone doesn’t know after that its up to other users that
they will assist, so here it occurs.
Hi there, yes this article is truly good and I have learned lot of things from it regarding blogging.
thanks.
I am regular reader, how are you everybody? This paragraph posted at this site is in fact pleasant.
https://xn--kr-ro2i3ru49at1jokm7ma.mystrikingly.com/blog/7f0769977d3
Thanks for finally writing about > JS 依照元素出現數量排序 – 馬老師 雲端研究室 < Loved it!
I will immediately grasp your rss as I can’t to find your email subscription link or newsletter service.
Do you have any? Please allow me recognize so that I could subscribe.
Thanks.
How To Get Better Results From Your Mesothelioma
Attorneys mesothelioma Law firms
This text is invaluable. Where can I find out more?
hi guys i bruit about that https://wplgsturapwmestv185.ru/
Hi there, just became aware of your blog through Google, and found
that it’s really informative. I’m gonna watch out for brussels.
I’ll appreciate if you continue this in future.
Numerous people will be benefited from your writing.
Cheers!
http://sweetbonanza.network/# sweet bonanza demo turkce
There’s definately a lot to learn about this issue.
I love all the points you’ve made.