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
http://scbist.com/blogs/babuler102/4718-so-skolki-let-delayut-korrekciyu-zreniya.html
how to prevent hair loss while on tamoxifen: tamoxifen 20 mg tablet – tamoxifen chemo
http://cytotec.club/# buy cytotec online fast delivery
get cheap propecia cost propecia without dr prescription order propecia pills
Лечение лудомании в Алматы https://someblog.ru/
http://nolvadex.life/# tamoxifen joint pain
Could Double Glazed Units Near Me Be The Key For 2023’s Challenges?
replacement Double glazed units near me
https://nolvadex.life/# tamoxifen bone pain
Could Jobs Working Remotely Be The Key To 2023’s Resolving?
get paid to work From home
Introduction https://wikiromandie.org/index.php?title=Utilisateur:TrimKetoBoostIngredients is a popular supplement that claims to help individuals achieve their weight loss goals by promoting ketosis in the body.
http://finasteride.store/# cost of cheap propecia now
Why Nobody Cares About Motor Vehicle Attorney Motor vehicle accidents
Who Is Renault Master Key Programming And Why You Should
Care car
How Much Can Double Glazing Offers Near Me Experts Earn? double glazed window Repairs – https://goldberg-manning-2.federatedjournals.com/ten-things-everybody-is-uncertain-about-double-Glazing-repair-near-me/ –
http://cytotec.club/# cytotec buy online usa
How To Become A Prosperous Misted Double Glazing Repairs Near Me When You’re Not Business-Savvy Double Glazing Unit Replacement
14 Cartoons On Player Favorite Slots Which Will Brighten Your Day Slot games
You’ll Never Be Able To Figure Out This Adhd In Adults Treatment’s
Secrets Adhd In Adults Treatment
cipro online no prescription in the usa: cipro generic – purchase cipro
You’ll Never Guess This Upvc Window Repair’s Tricks window repair (http://winen.kr/bbs/Board.php?bo_table=free&wr_id=16892)
I am really loving the theme/design of your blog.
Do you ever run into any internet browser compatibility problems?
A handful of my blog readers have complained about my site not working correctly in Explorer but looks great in Opera.
Do you have any tips to help fix this issue?
Hurrah, that’s what I was exploring for, what a
material! existing here at this webpage, thanks admin of
this site.
The Unspoken Secrets Of Tommy Hilfiger Crossbody Handbag Tommy hilfiger Handbags black
Why You Should Not Think About The Need To Improve Your 12kg Washing Machine best 12kg washing machine
buy cytotec over the counter: Misoprostol 200 mg buy online – order cytotec online
5 Killer Quora Answers On Replacement Double Glazing Units Near
Me double glazing units near me (Benito)
7 Secrets About Why Are CSGO Skins Going Up In Price
That Nobody Can Tell You Cs2 case; crabpigeon83.Bravejournal.net,
https://lisinopril.network/# order lisinopril 10 mg
Seven Reasons Why Accident Case Is Important accident lawsuit (Madeline)
9 Signs That You’re The Adhd Treatment Without Medication Expert Cost Of Adhd Medication Uk