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://www.oborudovanie-konferenc-zalov11.ru/ .
конференц зал оборудование оснащение конференц зал оборудование оснащение .
5 Killer Quora Answers On Shopping Online Sites List Shopping online Sites list
length of perscription for cleocin
Hi, constantly i used to check web site posts here in the early hours in the daylight, as i enjoy to find out more and more.
online slot
Hi fantastic blog! Does running a blog like this require a
large amount of work? I have very little understanding of
programming but I was hoping to start my own blog in the near future.
Anyhow, if you have any recommendations or tips for new blog owners please
share. I understand this is off topic but I just needed to ask.
Appreciate it!
See What Can I Buy From A Uk Website Tricks The Celebs Are Using Can I Buy From A Uk Website
where buy forxiga no prescription
Why You Should Forget About Making Improvements To Your Window Companies Bedford patio doors bedfordshire; kwalificaties.s-bb.nl,
Hi to every one, it’s really a pleasant for me to
visit this web page, it includes helpful Information.
оборудование конференц зала оборудование конференц зала .
[url=http://ubrus.ru/misc/farbtastic/static/html/3/1/1/54_kovanie_skameyki_iziskannoe_sochetanie.html]http://ubrus.ru/misc/farbtastic/static/html/3/1/1/54_kovanie_skameyki_iziskannoe_sochetanie.html[/url]
[url=https://moskva.skidkom.ru/partner/hobbika-kompaniya-po-proizvodstvu-sadovo-parkovoy-mebeli/skameyki-dlya-razdevalok/]https://moskva.skidkom.ru/partner/hobbika-kompaniya-po-proizvodstvu-sadovo-parkovoy-mebeli/skameyki-dlya-razdevalok/[/url]
[url=https://www.volzsky.ru/press-relize.php?id=19440]https://www.volzsky.ru/press-relize.php?id=19440[/url]
[url=https://siding-rdm.ru/kak-vybrat-idealnuju-kovanuju-sadovuju-skamejku-dlya-vashego-uchastka/]https://siding-rdm.ru/kak-vybrat-idealnuju-kovanuju-sadovuju-skamejku-dlya-vashego-uchastka/[/url]
can i buy cheap lipitor without a prescription
If you are going for most excellent contents like me, just pay a visit this web site daily because it offers feature contents, thanks
Troy Roofing Pros exemplifies excellence in the roofing industry,
boasting top certifications from leading manufacturers. Their meticulous
approach ensures each project in Troy, Michigan is executed
flawlessly, prioritizing client safety and reputation. With skilled crews and a commitment to perfection, they stand as the premier choice for roofing
needs in the area.
Medicament information for patients. Brand names.
where to get cheap macrobid no prescription
Everything news about medicine. Read information here.
Hello! get finasteride without a prescription beneficial web site.
The Secret Secrets Of Ai Article Rewriter ai rewrite Article
Beware Of This Common Mistake When It Comes To Your Slot Games Video Slots (https://Www.Lipkko.Co.Kr)
Right aѡay I am ɡoing to do my breakfast, onche һaving myy breakfast cⲟming ᧐ver again to
rad furtһer news.
Check out my blog post; oto slot link alternatif
Are you looking for a solution to erectile dysfunction? Look no further! can i buy viagra connect at walgreens is here to help.
Meds information leaflet. Long-Term Effects.
buy generic singulair
Some what you want to know about medicine. Get information now.
Why You Should Concentrate On Improving Birth Injury Compensation birth injuries
Your Family Will Thank You For Getting This Generalized Anxiety
Disorder Causes of generalized anxiety disorder
can you get generic sildigra pill
Guide To Upvc Windows Near Me: The Intermediate Guide To Upvc Windows Near Me
Upvc Windows Near Me
10 Things Everybody Has To Say About 3 Wheel Mobility Scooters Near Me mobility Trike Scooters (qpony.pl)
Guide To Double Glazing Near Me: The Intermediate Guide To Double Glazing
Near Me double Glazing near me
Guide To Double Glazed Window Near Me: The Intermediate
Guide For Double Glazed Window Near Me Double Glazed Window Near Me, http://Www.Worryfreecomputers.Com/Tube/Index.Php?Q=AHR0CHM6Ly9HcnRpY2Xlc2NhZC5Jb20VMjQtaG91CnMtdG8TaW1Wcm92AW5NLWRvdWJsZS1NbGF6ZWQtZG9VcnMtbmVhci1TZS00MDQ4MjcuaHRtbA,