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 awesome to pay a quick visit this site and reading the views of all friends concerning
this paragraph, while I am also eager of getting know-how.
Thanks for every other great post. The place else may anybody
get that type of info in such a perfect way of writing? I have a presentation next week, and I’m at the search for such
info.
my page: herpesyl results
My brother suggested I might like this blog. He was totally right.
This post truly made my day. You cann’t imagine just how much time I
had spent for this information! Thanks!
Pretty! This has been an incredibly wonderful post.
Many thanks for providing this information.
This Is How Lexus Key Maker Will Look In 10 Years’ Time
Lexus Rx300 Key Replacement
I got this web site from my buddy who shared with me concerning this
site and now this time I am browsing this site and reading very informative posts at
this place.
https://sweetbonanza.network/# sweet bonanza slot
This design is spectacular! You definitely know how to keep a reader amused.
Between your wit and your videos, I was almost moved
to start my own blog (well, almost…HaHa!) Fantastic job.
I really enjoyed what you had to say, and more
than that, how you presented it. Too cool!
I think this is among the most significant info for me.
And i am glad reading your article. But should remark on few general things, The site style is great, the articles is really excellent :
D. Good job, cheers
Thanks for finally writing about > JS 依照元素出現數量排序 – 馬老師 雲端研究室 < Loved it!
Whats up very nice website!! Guy .. Excellent .. Wonderful ..
I will bookmark your blog and take the feeds additionally?
I’m happy to seek out so many helpful information right here within the post, we’d
like develop more strategies on this regard, thank you for sharing.
. . . . .
I am regular visitor, how are you everybody? This piece of writing posted at this web page is in fact pleasant.
lesbian porn
https://xn--km-ro2i3ru49at1jokm7ma.mystrikingly.com/blog/f9ec04ab18e
Thank you for the auspicious writeup. It in fact was a amusement account it.
Look advanced to far added agreeable from you! However, how can we
communicate?
What’s up Dear, are you actually visiting this web page on a regular basis, if
so after that you will definitely take fastidious experience.
buy viagra online
Attractive section of content. I just stumbled upon your web site and in accession capital to assert that I get actually enjoyed account your blog posts.
Any way I will be subscribing to your feeds and even I achievement you access consistently fast.
http://slotsiteleri.bid/# slot siteleri
Pretty! This has been a really wonderful post. Many thanks for supplying this information.
What Freud Can Teach Us About Replacement Fiat 500 Key fiat 500x Key fob Replacement
en iyi slot siteleri: en guvenilir slot siteleri – en iyi slot siteleri 2024
Blingle Jacksonville, FL
10221-3 Beach Boulevard,
Jacksonville, FL 32246, United Ꮪtates
9049778928
Permanent outdoor light inspiration
http://sweetbonanza.network/# sweet bonanza kazanc
What’s The Job Market For Car Key Cover Fiat 500 Professionals
Like? car key Cover Fiat 500
It’s in point of fact a nice and useful piece of information. I’m happy that you shared this helpful info with
us. Please stay us informed like this. Thanks for sharing.
en guvenilir slot siteleri: en iyi slot siteleri – slot siteleri
Нi thегe! I’m at ᴡork surfing aгound ʏour blog from my new ipphone 3gs!
Juѕt wanteԀ tο say I love reading tһrough yoսr blog аnd ⅼook forward to all your posts!
Carry ߋn the excellent ᴡork!
Lߋok at mу web site – situs togel resmi
en guvenilir slot siteleri: slot casino siteleri – slot casino siteleri
What a information of un-ambiguity and preserveness of
valuable experience about unpredicted feelings.