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
https://canadaph24.pro/# canadadrugpharmacy com
Spot on with this write-up, I absolutely believe that this website needs far more attention. I’ll
probably be returning to see more, thanks for the information!
canadian pharmacy uk delivery Prescription Drugs from Canada pharmacies in canada that ship to the us
В Покердом понимают, что удовлетворенность клиентов является ключом к успешному онлайн-казино.
purple pharmacy mexico price list: mexican pharmacy – mexico pharmacies prescription drugs
http://mexicoph24.life/# mexican drugstore online
mexico drug stores pharmacies Online Pharmacies in Mexico mexico pharmacy
http://canadaph24.pro/# cheap canadian pharmacy online
best online pharmacy india online shopping pharmacy india online shopping pharmacy india
Why Handbags Tommy Hilfiger Is The Next Big Obsession Tommy hilfiger handbag tote
7 Tips About Avon Latest Book That Nobody Will Tell
You avon book September 2023
mexico pharmacy: Online Pharmacies in Mexico – buying from online mexican pharmacy
mexico pharmacy Online Pharmacies in Mexico mexican online pharmacies prescription drugs
9 . What Your Parents Teach You About Upvc Windows
Near Me upvc Windows near me
buy medicines online in india https://indiaph24.store/# Online medicine home delivery
reputable indian online pharmacy
http://mexicoph24.life/# medicine in mexico pharmacies
10 Facts About Volkswagen Key Cutting Service Near Me That Will Instantly Bring You To A
Happy Mood volkswagen spare key – Sherita,
15 Double Glazed Window Repair Benefits You
Should All Be Able To uk
What’s The Current Job Market For Double Glazing Door Repairs Professionals?
door Repair
Магистерский диплом пишут
по таким направлениям, как экономика в нашей стране и финансы, юриспруденция,
педагогика и психология,.
My page :: http://academijacrimea.ru/index.php?subaction=userinfo&user=ekomup
15 Reasons You Shouldn’t Ignore Window Repairs
Near Me Upvc Window Repairs Near Me
Why Double Glazed Repairs Near Me Isn’t A Topic That People Are Interested In Double Glazed Repairs Near Me double glazed window handles
What Is Double Glazing Doors Near Me And Why You Should
Take A Look double glazed units
reputable indian online pharmacy http://indiaph24.store/# best india pharmacy
world pharmacy india
http://mexicoph24.life/# buying prescription drugs in mexico
best online pharmacies in mexico: mexico pharmacy – mexican drugstore online
http://mexicoph24.life/# mexican mail order pharmacies
Five Killer Quora Answers On Designer Handbags For Cheap Designer handbags for cheap
Simply wish to say your article is as astonishing.
The clarity in your post is simply great and i can assume you’re an expert on this subject.
Fine with your permission let me to grab your RSS feed to keep updated with forthcoming post.
Thanks a million and please carry on the gratifying work.
Про бонусы есть, турниры, даже про новости можно узнать.