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
14 Smart Ways To Spend Extra Money Marc Jacobs Bag Tote Budget Marc Jacobs tote bag pink
Will Upvc Window Repairs Never Rule The World?
window repairs near me – Leslee,
how to prevent hair loss while on tamoxifen tamoxifen brand name tamoxifen cancer
5 Killer Quora Answers To Treadmills UK Reviews treadmills uk reviews (0553721256.ussoft.kr)
Hello! I could have sworn I’ve been to this site before but after looking at
many of the articles I realized it’s new to me. Nonetheless, I’m
definitely happy I found it and I’ll be bookmarking it and checking back frequently!
20 Insightful Quotes About Work From Home Jobs No Experience best work from home jobs (Mickie)
where can i buy cipro online cipro online no prescription in the usa ciprofloxacin generic price
lisinopril 1.25 mg: lisinopril india price – lisinopril 40 mg without prescription
I every time used to read article in news papers
but now as I am a user of net so from now I am using net for
content, thanks to web.
14 Questions You’re Insecure To Ask About Laundry Washer And Dryer Combo laundry Washer and Dryer Combo
Could Window Repair Near Be The Answer To 2023’s Resolving?
upvc window repair near Me
https://lisinopril.network/# lisinopril cost canada
10 Facts About Malpractice Litigation That Will Instantly Make
You Feel Good Mood malpractice lawyers, Winnie,
buy propecia for sale: propecia for sale – cost of cheap propecia prices
Abortion pills online п»їcytotec pills online buy cytotec over the counter
zestoretic 10 12.5 mg buy zestril 20 mg online order lisinopril 10 mg
Why You Should Not Think About Making Improvements To Your Washer
Tumble Dryer Combo washer tumble dryer Combo
SEO раскрутка сайта в топ https://seositejob.ru/ Яндекс и Google от профессионалов.
buy cheap propecia prices: order propecia without a prescription – propecia without prescription
Also visit my web blog: top ten poker sites
order cytotec online buy cytotec over the counter п»їcytotec pills online
Second Hand Mobility Scooters For Sale Explained In Fewer Than 140 Characters Second hand electric mobility scooters for sale
https://cytotec.club/# cytotec pills buy online
https://ciprofloxacin.tech/# п»їcipro generic
ciprofloxacin over the counter: cipro ciprofloxacin – buy cipro online
https://lisinopril.network/# zestoretic 10 mg
Since the admin of this web site is working, no hesitation very quickly it will be well-known, due to its quality contents.
“Ask Me Anything”: Ten Answers To Your Questions About Adhd Adult Treatment Adults Adhd Treatment
How To Become A Prosperous Washing Machines 12kg If You’re
Not Business-Savvy 12kg washers (Onita)
Check Out: How Cheapest 12kg Washing Machine Is Gaining Ground, And What You Can Do About It cheapest 12kg washing machine