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
treatments for ed http://mexicanpharm24.cheap/# mexico pharmacies prescription drugs
The Best Accident Injury Lawyers Near Me Tricks To Transform Your Life
best accident injury lawyers
7 Small Changes You Can Make That’ll Make The Biggest
Difference In Your Private Adult ADHD Assessment private adhd Assessment guildford
12 Companies Setting The Standard In Audi Car Key
Replacement audi a3 key (Loretta)
Hello there, just became alert to your blog through Google,
and found that it is really informative. I
am going to watch out for brussels. I will be grateful if you continue this in future.
Numerous people will be benefited from your writing.
Cheers!
The 10 Most Terrifying Things About ADHD Assessment Private adhd assessment private
купить аттестат занесенный в реестр prema-diploms.ru .
should note that https://wakelet.com/wake/1JB41pLos8NVGmDqEZyhg are so called because they use the ip address of the mobile network.
It’s an amazing paragraph designed for all the online users;
they will take benefit from it I am sure.
My homepage :: https://www.cucumber7.com/
20 Insightful Quotes About Mesothelioma Attorney mesothelioma law firms (https://opensourcebridge.science)
10 Beautiful Graphics About Private Psychiatrist London private psychiatry near me
ed treatments: cheap prescription drugs – cheap pills online
Speak “Yes” To These 5 Mystery Boxes Tips Best Mystery Box Sites
https://mexicanpharm24.cheap/# buying prescription drugs in mexico
prescription drugs online without doctor
заказать дипломную работу цена prema-diploms.ru .
reputable indian online pharmacy: indian pharmacy purchase online – top online pharmacy india
20 Myths About Item Upgrades: Dispelled Item upgrader kit
mexican pharmaceuticals online: mexico pharmacy cheap – medication from mexico pharmacy
Официальная покупка диплома вуза с сокращенной программой в Москве
Официальное получение диплома техникума с упрощенным обучением в Москве
Thank you. Valuable stuff!
https://indianpharm24.pro/# Online medicine order
ed aids
Volvo V50 Key Fob Strategies That Will Change Your Life volvo s40 key (Esperanza)
Three Reasons Why Three Reasons Your Wine Fridge Is Broken (And How To Fix It) small wine
fridge uk (express-page.com)
buruan daftar di situs slot gacor bonekslot sudah dijamin paling gacor tidak ada tandingannya
Свой инет-магазин “Мебель-шмммммммебель” зовет широченный ассортимент мебели чтобы кухонь. У нас вы выищете шиздец необходимое для образования приятного и функционального внутреннего убранства http://tyfapao6kuhnishki.ru.
3 Reasons You’re ADHD Private Diagnosis Is Broken (And How To Repair It) private adhd assessment kent
аттестат за 9 класс цена prema-diploms.ru .
how to get prescription drugs without doctor: cheap pharmacy online – viagra vs cialis bodybuilding
10 Quick Tips For Daftar Akun Togel Resmi togel singapore (3.13.251.167)