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
Are New Slots Online The Best Thing There Ever
Was? latest slots (Lela)
buy cytotec over the counter buy cytotec pills buy cytotec
A Complete Guide To Auto Accident Injury Attorney auto accident lawyer no injury
https://lisinopril.network/# lisinopril 5 mg for sale
7 Simple Tips For Rocking Your Repairs To Double Glazed Windows Double glazed Window repairs Near Me
http://nolvadex.life/# tamoxifen headache
How To Explain Secondary Double Glazing Near Me To Your Boss
company
https://cytotec.club/# Misoprostol 200 mg buy online
What’s The Current Job Market For Designer Handbags Green Professionals
Like? designer handbags green – http://littleyaksa.Yodev.net/bbs/board.php?bo_table=free&wr_id=6048753 –
I think the admin of this site is genuinely working hard for his website, as here every
information is quality based information.
This Is The Ugly Reality About Accident Accident Lawsuits
Hi, I do think this is a great blog. I stumbledupon it ;
) I am going to come back yet again since I book-marked
it. Money and freedom is the best way to change, may you be
rich and continue to help other people.
https://cytotec.club/# buy cytotec
Nice post. I was checking constantly this blog and I’m
impressed! Very useful information particularly the final phase
🙂 I take care of such information a lot.
I was looking for this certain info for a very long time.
Thank you and good luck.
How Defra Stove Changed Over Time Evolution Of Defra Stove defra stoves – Beau –
20mg prednisone
http://cytotec.club/# purchase cytotec
The 10 Scariest Things About Medical Malpractice Attorneys Medical malpractice attorneys
10 Life Lessons We Can Take From Best Mesothelioma Compensation Lawyers compensation for Mesothelioma victims
14 Cartoons On Replace Mazda Key Which Will Brighten Your Day
mazda Key replacement
Unquestionable Evidence That You Need Volvo V40 Key replacement Key for volvo v50
A Trip Back In Time The Conversations People Had About Private Psychiatrist Cardiff Cost
20 Years Ago psychiatrist Manchester Private
A Trip Back In Time How People Talked About Mesothelioma Lawsuit 20 Years Ago mesothelioma settlement (Hallie)
lisinopril uk buy lisinopril 20 mg without prescription lisinopril 40 mg for sale
http://nolvadex.life/# nolvadex vs clomid
order cheap propecia pill: buying cheap propecia online – cost generic propecia without insurance
The Best Motor Vehicle Settlement Tricks To Transform Your Life motor vehicle accident lawyers
See What Car Key Replacement Near Me Tricks The Celebs Are Using car key replacement near me
lisinopril 10 best price cost of prinivil zestril 5 mg tablets
cytotec pills buy online: buy cytotec online – buy misoprostol over the counter