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
You’ll Be Unable To Guess Double Glazed Replacement Windows’s Tricks double glazed replacement windows
india pharmacy mail order http://indiaph24.store/# п»їlegitimate online pharmacies india
pharmacy website india
india pharmacy http://indiaph24.store/# world pharmacy india
top online pharmacy india
The 10 Most Terrifying Things About Adhd In Older Females Symptoms adhd in Older females symptoms
Guide To Link Alternatif Gotogel: The Intermediate Guide
Towards Link Alternatif Gotogel Link Alternatif Gotogel
10 Things Everybody Has To Say About Loveseat For Sale Loveseat For Sale
couches recliner [https://Articlement.com/]
The 10 Worst Avon Welcome Kit Fails Of All Time Could Have Been Prevented avon welcome kit
http://canadaph24.pro/# canadian discount pharmacy
mexican drugstore online: Online Pharmacies in Mexico – mexico drug stores pharmacies
п»їbest mexican online pharmacies: Online Pharmacies in Mexico – mexican rx online
http://canadaph24.pro/# onlinepharmaciescanada com
indian pharmacy online: buy medicines from India – cheapest online pharmacy india
reputable canadian pharmacy canadian pharmacies recommended canadian pharmacies
buying prescription drugs in mexico: cheapest mexico drugs – mexican online pharmacies prescription drugs
What Is All CSGO Skins And Why Is Everyone Speakin’ About It?
Cs go
20 mg lisinopril tablets
https://canadaph24.pro/# trusted canadian pharmacy
Online medicine order https://indiaph24.store/# indian pharmacy
indianpharmacy com
What’s The Current Job Market For Upvc Window Handle Replacement Professionals?
Window Handle Replacement
mexico pharmacy buying from online mexican pharmacy buying prescription drugs in mexico online
9 Signs That You’re A Mesothelioma Compensation Expert mesothelioma attorney (http://mspeech.Kr/bbs/board.php?bo_table=705&wr_Id=196429)
https://mexicoph24.life/# mexican mail order pharmacies
Attractive section of content. I just stumbled upon your
web site and in accession capital to assert that I acquire in fact
enjoyed account your blog posts. Anyway I will be subscribing to
your feeds and even I achievement you access consistently rapidly.
5 Designer Handbags And Purses Myths You Should Stay Clear
Of Designer Handbags Names
buy prescription drugs from india indian pharmacy best india pharmacy
15 Reasons Not To Ignore Under Desk Treadmill With Incline treadmill for under desk
Five Killer Quora Answers On Replacement Double Glazing Units Near Me replacement double Glazing units near Me
See What Seo Backlink Building Software Tricks The
Celebs Are Using Seo backlink building software
http://canadaph24.pro/# northern pharmacy canada
https://canadaph24.pro/# canada online pharmacy