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
A Journey Back In Time What People Talked
About Audi A4 Car Key Replacement 20 Years Ago audi Key replacement (ugzhnkchr.ru)
купить диплом инженера строителя server-diploms.ru .
I think the admin of this web page is genuinely working hard for his website,
since here every material is quality based stuff.
Feel free to surf to my homepage 리니지 프리서버 커뮤니티
купить скан диплома server-diploms.ru .
india pharmacy: India pharmacy delivery – pharmacy website india
The Reasons You’re Not Successing At Bentley Car Keys near By
Порнуха
купить диплом мвд server-diploms.ru .
I’m now not certain where you’re getting your
info, however good topic. I needs to spend a while finding
out more or working out more. Thanks for magnificent info I used to be searching for this info for my mission.
What’s The Job Market For Light Wheelchairs Folding Professionals Like?
Folded wheelchair
What’s Taking place i’m new to this, I stumbled
upon this I’ve discovered It absolutely helpful and
it has aided me out loads. I am hoping to give a contribution & help
different customers like its helped me. Great job.
вавада казино официальный сайт рабочее зеркало
mexico drug stores pharmacies Mexican pharmacy ship US mexico drug stores pharmacies
Hi to every single one, it’s genuinely a pleasant for
me to pay a quick visit this site, it includes priceless
Information.
This website certainly has all of the information I needed about this subject and didn’t know who
to ask.
Пошаговая инструкция по официальной покупке диплома о высшем образовании
over the counter ed cheap drugs online ed solutions
10 Meetups On Built-In Fridge You Should Attend refridgerator uk [https://utahsyardsale.com]
ed drug prices cheaper medications male enhancement
I have been checking out some of your articles and
i can state pretty clever stuff. I will make surre to bookmark your
website.
Here is myy site: Color Cast In Photoshop
Drugs prescribing information. Effects of Drug Abuse.
can i get generic benicar for sale
All what you want to know about medicine. Get here.
The 10 Most Terrifying Things About Leather Recliners For Sale leather Recliners For Sale (minecraftcommand.science)
We are a gaggle of volunteers and opening a new scheme
in our community. Your web site offered us with valuable information to work on. You’ve done a formidable task
and our entire community shall be thankful
to you.
продажа дипломов куплю диплом server-diploms.ru .
Why Bus Driving License Should Be Your Next Big Obsession prawo
jazdy kat b cena – http://www.stes.tyc.edu.tw –
Thanks designed for sharing such a good opinion, paragraph is fastidious, thats why i have read it entirely
The Top 5 Reasons People Win At The Pragmatic Image Industry 프라그마틱 공식홈페이지 (http://www.google.com.Om)
new ed drugs https://indianpharm24.pro/# reputable indian pharmacies
Why No One Cares About 4d Result Sport Toto 토지노
사이트 (Michelle)
10 Myths Your Boss Has Concerning Cost For Spare Car Key car keys lost no spare (Sandra)