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
Remarkable! Its in fact awesome post, I have got much clear idea
concerning from this article.
Helpful info. Fortunate me I found your web site by accident, and I’m
stunned why this accident didn’t happened earlier!
I bookmarked it.
A motivating discussion is definitely worth comment.
I do think that you ought to publish more on this issue, it may not be a taboo matter but typically folks don’t talk about these issues.
To the next! Best wishes!!
It’s the best time to make some plans for the future and
it is time to be happy. I’ve read this post and if I could I want to suggest
you some interesting things or tips. Perhaps you could write next articles referring to this article.
I want to read even more things about it!
penis enlargement
slot siteleri 2024: canl? slot siteleri – slot siteleri
en iyi slot siteler: slot oyun siteleri – deneme bonusu veren siteler
https://denemebonusuverensiteler.win/# bahis siteleri
7 Simple Strategies To Completely Refreshing Your Mesothelioma Lawsuits Mesothelioma Attorney
deneme veren slot siteleri: yasal slot siteleri – yeni slot siteleri
http://slotsiteleri.bid/# slot siteleri guvenilir
I visit everyday some sites and sites to read posts, however this blog provides feature based
writing.
My blog … Daycare Near Me – Find The Best Daycares Near You
https://sweetbonanza.network/# sweet bonanza free spin demo
Hi there, for all time i used to check blog posts here early in the dawn, since i
like to find out more and more.
https://denemebonusuverensiteler.win/# deneme bonusu
5 Must-Know-Practices Of Mesothelioma For 2023 asbestos-related Cancer attorney
Pretty nice post. I just stumbled upon your weblog
and wished to say that I have truly enjoyed surfing
around your blog posts. In any case I will be subscribing
to your rss feed and I hope you write again soon!
Здравствуйте!
Мы изготавливаем дипломы любой профессии по доступным тарифам.
asteroid-bowling.ru/index
It is truly a great and helpful piece of info. I am glad that
you shared this useful information with us.
Please stay us informed like this. Thank you for sharing.
Aw, this was a very nice post. Finding the time and actual effort to generate a very good article… but what can I say… I hesitate a lot and never seem to get nearly anything done.
Greetings! Quick question that’s totally off topic.
Do you know how to make your site mobile friendly?
My site looks weird when viewing from my iphone. I’m
trying to find a theme or plugin that might be able to resolve this issue.
If you have any recommendations, please share. Thank you!
15 Tips Your Boss Wished You’d Known About Mesothelioma Lawyer Mesothelioma law advocate
Fantastic blog! Do you have any tips for aspiring writers?
I’m planning to start my own site soon but I’m a little lost on everything.
Would you advise starting with a free platform like WordPress or go for a paid option?
There are so many options out there that I’m completely confused ..
Any suggestions? Appreciate it!
Wow, this article is nice, my sister is analyzing these kinds of
things, thus I am going to convey her.
Excellent weblog here! Also your website rather a lot up fast!
What web host are you the usage of? Can I am getting
your associate hyperlink to your host? I wish my
site loaded up as fast as yours lol
Everything is very open with a precise clarification of the
issues. It was definitely informative. Your website is very helpful.
Many thanks for sharing!
http://denemebonusuverensiteler.win/# deneme bonusu veren siteler
fantastic post, very informative. I’m wondering why the other specialists
of this sector do not realize this. You should continue
your writing. I am confident, you have a huge readers’ base already!
Very soon this web page will be famous among all blog people, due to
it’s pleasant content