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
The Most Hilarious Complaints We’ve Received About Leather Sofa
reupholster Leather Couch
20 Great Tweets From All Time About ADHD Untreated In Adults Symptoms Of Adhd In Adults Treatment
https://telegra.ph/Your-Gateway-to-Localized-Online-Casino-Fun—Glory-Casino-Bangladesh-06-29
Great work! That is the kind of info that should be shared
around the internet. Shame on Google for no longer positioning this put up upper!
Come on over and visit my website . Thank you =)
Are You Responsible For A ADHD Private Assessment Budget?
10 Unfortunate Ways To Spend Your Money Adhd Assessment Private uk
Lorem Ipsum has been the industry’s standard dummy text https://breakoutseason.es/wllqvl of type and scrambled it to make a type specimen book.
This Week’s Most Popular Stories About SEO Consultant London SEO
Consultant London best seo services london (https://horsetrailerworld.com/Abuse/?type=trailer&id=1475661&url=https://gorod-lugansk.com/User/denaction72)
Ten 18 Wheeler Accident Law Firms That Really Change Your Life 18 wheeler
accident law firms (m.www.towelking.co.kr)
Subscribers who sign multiple contracts receive more capital or control over key functions of the https://blast.io-event.network/mainnet/ protocol in the main network.
Don’t Buy Into These “Trends” About Asbestos Attorney
Mesothelioma cassylawn.top
You’ll Be Unable To Guess Online Shopping Uk Sites’s Benefits Online shopping uk sites
11 “Faux Pas” That Are Actually Okay To Create With Your Single Pushchair With Buggy Board single pushchairs (https://covers.syracuse.cloud/Cover/ESCP/EXTR/xI1DjMxMA3yGnIXDgDNhsw2/9780691135038/MEDIUM?fallback=https://www.mineral-water.co.kr/system/board.php?bo_table=free&wr_id=44963)
15 Gifts For The Slot Promotions Lover In Your Life
exciting casino slots
cost of generic luvox for sale
Your method of describing everything in this article is really fastidious, every one can easily be aware of it, Thanks a lot.
Dangerous Drugs Lawyers: It’s Not As Difficult As You Think Dangerous Drugs attorneys
how much is too prednisone
Приветствую. Подскажите, где найтиполезные блоги о недвижимости? Пока нашел https://universal37.ru
Guide To Slot Payouts: The Intermediate Guide
For Slot Payouts Slot payouts (m.Asdf.blackpeach.co.kr)
I am not sure where you’re getting your information, but good topic.
I needs to spend some time learning much more or understanding more.
Thanks for excellent info I was looking for this info for my mission.
Do Not Make This Blunder With Your Slot Wins video poker Slots
Hi there mates, nice piece of writing and nice urging commented at this place,
I am genuinely enjoying by these.
This is a great tip particularly to those new to the blogosphere.
Short but very precise info… Many thanks
for sharing this one. A must read article!
It’s remarkable to go to see this web site and reading the views of all mates
concerning this piece of writing, while I am also zealous
of getting know-how.
Interactive Slots Tools To Make Your Daily Life Interactive Slots Trick That Every Person Should Know Interactive slots (rsmegane.com)
Pills information for patients. What side effects?
how to buy cheap propecia no prescription
Best about meds. Read now.
Could Best Online Shopping Sites Clothes Be The Answer To 2023’s Resolving?
vimeo
porn (fr.lporn.club)
can you get aldactone without insurance
cost of generic zithromax online