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
Why Motor Vehicle Claim Is Right For You? Motor Vehicle Accident
Attorneys, Eng.Worthword.Com,
How To Explain Desk Treadmill To Your Mom small under desk treadmill (o80b27ibxncian6alk72Bo38c.kr)
Five Killer Quora Answers To Double Glazed Window Repairs Near Me Double Glazed Window Repairs Near Me
If some one wishes expert view regarding blogging and site-building after that
i advise him/her to visit this website, Keep up the nice job.
You expressed that adequately.
https://nolvadex.life/# nolvadex during cycle
cost cheap propecia no prescription: order cheap propecia without prescription – propecia generics
Misoprostol 200 mg buy online: buy cytotec online – order cytotec online
propecia sale: how cÉ‘n i get cheap propecia pills – order generic propecia without prescription
Could Avon Representative Login Uk Be The Key To Achieving 2023?
avon uk login Rep (cwit.edu.Sa)
Appreciating the time and energy you put into your site and detailed information you present.
It’s nice to come across a blog every once in a while that isn’t
the same unwanted rehashed information. Fantastic read!
I’ve bookmarked your site and I’m including
your RSS feeds to my Google account. https://migration-bt4.co.uk/profile.php?id=231521
https://finasteride.store/# cheap propecia online
For newest information you have to pay a quick visit web and on the web I found this web
page as a most excellent site for hottest updates.
See What Car Accident Lawsuit Tricks The Celebs Are Using Car accident Lawsuit
raloxifene vs tamoxifen tamoxifen bone density benefits of tamoxifen
where to buy nolvadex: tamoxifen 20 mg tablet – what is tamoxifen used for
The Best Advice You Could Receive About Which CSGO Case Is The Most Profitable Danger Zone Case
lisinopril 12.5 cost of lisinopril 30 mg lisinopril 3.5 mg
http://ciprofloxacin.tech/# buy cipro
http://lisinopril.network/# buy cheap lisinopril 40 mg no prescription
Abortion pills online purchase cytotec buy cytotec
hello!,I love youur wrіting so a ⅼot!
share we keep up a ϲⲟrrfespondence extra approximately youг
article on AOᒪ? I need a specialist on this space to unravel
my proƄlem. May be that is you! Tаking a look forward to
look you.
my webpage info lengkap
Are The Advances In Technology Making Case CSGO Sites Better Or Worse?
case Revolver
tamoxifen for breast cancer prevention: does tamoxifen cause joint pain – tamoxifenworld
11 Creative Ways To Write About Replacement Double Glazing Units Near Me Replacement Double Glazing Unit
buying cheap propecia price get propecia without insurance cost generic propecia for sale
The 10 Most Terrifying Things About Affordable Local SEO Services Near
Me affordable local seo services near me
15 Things You Don’t Know About Google Local SEO Local seo agencies
7 Secrets About Online Shopping Sites Top 7 That No One
Will Tell You Vimeo
buy cytotec in usa cytotec online buy cytotec in usa