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
dark markets thailand darknet market lists
I like the helpful information you provide in your articles.
I’ll bookmark your blog and check again here frequently.
I’m quite sure I’ll learn a lot of new stuff
right here! Best of luck for the next!
tfmpp pills dark market links
%%
Also visit my blog post … vegan gummies; http://soosunglift.gabia.io/,
%%
Also visit my website – sexdoll sale
%%
Feel free to surf to my website :: Business investors in south africa
%%
my blog post … business opportunities In Africa
Soma (carisoprodol) can be taken as needed. It starts
to work pretty quickly, and you don’t need to take it consistently for it
to be effective.
Capsule Icon
Soma (carisoprodol) should only be used for
a short period of time. It’s not meant to be used long-term because the types
of pain it treats should not be chronic. If you have
chronic muscle spasms, there might be a better alternative for you.
best darknet marketplaces reddit darknet markets noobs
I blog often and I seriously thank you for your content.
Your article has truly peaked my interest. I will take a note of your website and keep checking for new information about once per week.
I subscribed to your RSS feed too.
what is darknet markets darknet market sites
мега шишки mega market
darknet market drug bohemia market link
dark web sites name list dark web links
darknet market reviews superman pills mg
my canadian pharmacy canadian pharmacy for viagra
darknet vendor reviews dark markets indonesia
https://cracksrate.com/
74cd785c74 justuzzi
diyarbakir hava durumu 15 günlük
dark web prostitution darknet markets still up
I know thіs if offf topic but I’m looking into starting my oԝn weblog
аnd ᴡas curious wһat ɑll іѕ neeeed too get ѕet up?
I’m assuming having a blog ⅼike үouгs ԝould ccost
а pretty penny? I’m not ѵery web smart sⲟ I’m not 100% ѕure.
Anny suggestions oг advice would Ьe greatly appreciated.
Thanks
Feel frree to visjt my һomepage: daftar slot online
%%
My web site :: Window Repairs near me
darknet drug market list dark web drug marketplace
My brother recommended I might like this blog.
He was totally right. This post truly made my
day. You cann’t imagine simply how much time I
had spent for this information! Thanks!
мега онион mega даркнет
deep web weed prices best darknet gun market
%%
Also visit my blog :: Betting
deep deep web links bitcoin darknet drugs
dark markets bolivia 2022 working darknet market
dark market blackweb darknet market