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
Brandbusters TShirt Layout Project Submission.
Also visit my web-site – Bookmarks
Quand une femme prend du Viagra homme: viagra sans ordonnance – Viagra Pfizer sans ordonnance
They have items to market or services to use.
Here is my site: Can a foundation apply for Grants
Real HGH Rewards (NewULife Hgh Gel Not Checked Yet).
Feel free to visit my website :: vigabatrin 500mg brand name
Genuine HGH Perks (NewULife Hgh Gel Not Examined Yet).
Visit my web site nib makeup foundation
Your way of telling the whole thing in this
article is genuinely pleasant, all be able to easily understand it, Thanks a lot https://boutiquelemarie.com/comment-combiner-les-chemises-et-les-cravates/
https://clssansordonnance.icu/# pharmacie en ligne sans ordonnance
A synthetic USP human growth hormonal agent (somatropin).
my blog … Is foundation necessary in makeup
60+ sensational instances of geometric layouts.
My blog post :: t shirt design website free
Viagra homme prix en pharmacie sans ordonnance: Sildenafil Viagra – Acheter viagra en ligne livraison 24h
Genuine HGH Perks (NewULife Hgh Gel Not Evaluated Yet).
my page – is foundation necessary in makeup
Typeography TShirt Layout Example Bootstrapping.
my web-site … Bookmarks
Typeography TShirt t shirt design template free pdf Example Bootstrapping.
Viagra gГ©nГ©rique pas cher livraison rapide: Viagra generique en pharmacie – Viagra sans ordonnance 24h Amazon
anon ig viewer anon ig viewer .
What a stuff of un-ambiguity and preserveness of valuable experience
about unexpected feelings.
My web page; provadent a scam
п»їpharmacie en ligne france: pharmacie en ligne france livraison belgique – pharmacie en ligne france pas cher
Prix du Viagra 100mg en France Viagra sans ordonnance 24h Meilleur Viagra sans ordonnance 24h
Individuals put on t-shirts to do everything: Stroll the canine.
Feel free to visit my blog post – Bookmarks
NewULife Ranks. Invite to my NewUlife Evaluation.
Feel free to visit my blog post Nib meaning in construction
In this case, the life insurance policy firm’s money.
Feel free to visit my website – Bookmarks
NewULife Ranks. Welcome to my NewUlife Evaluation.
Here is my web-site; Bookmarks
Просто торговля на интернет фишки рано или поздно перерастет в
проект на монеты и купюры, а вот перерастет ли игра со ставками в.
My web-site – https://rupokerredstar.ru/
Typeography TShirt Style Example Bootstrapping.
Review my homepage: t shirt design ideas pinterest
What is New U Life SOMADERM Transdermal Gel.
Feel free to surf to my blog Bookmarks
60+ stunning instances of geometric styles.
My web site … t Shirt design app for android
pharmacie en ligne france fiable: Acheter Cialis 20 mg pas cher – trouver un mГ©dicament en pharmacie
Howdy! I simply wish to give you a big thumbs up for your great information you’ve got right here on this post.
I am coming back to your website for more soon.
https://mymedshoptld24x7.shop/macrobid/
Good day! This is my 1st comment here so I just wanted to give a quick shout out and tell you I
genuinely enjoy reading through your posts.
Can you suggest any other blogs/websites/forums that
deal with the same topics? Thanks a lot!