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
How To Resolve Issues With Upvc Window Repairs Upvc window Repairs Near Me
india online pharmacy https://indiaph24.store/# buy prescription drugs from india
top online pharmacy india
Excellent post. I was checking continuously this blog
and I’m impressed! Very useful information specifically the last
part 🙂 I care for such info a lot. I was looking for this
certain information for a long time. Thank you and best of luck.
medicine in mexico pharmacies reputable mexican pharmacies online mexico drug stores pharmacies
Ten Upvc Window Repairs That Really Make Your Life Better upvc
window repairs (Benjamin)
How Double Bunk Has Transformed My Life The Better Two Double Bunk Beds
top online pharmacy india indian pharmacy top 10 pharmacies in india
10 Beautiful Images Of Sex Machines Store Sex Machine For Sale
What’s Holding Back The Mobility Scooter To
Buy Near Me Industry? places that buy mobility scooters
10 Strategies To Build Your Double Glazing Repairs Near Me Empire double glazing repair
5 Killer Quora Answers On Window And Door Replacement London door replacement London
canadian drugs online: canadian king pharmacy – canadian world pharmacy
A Time-Travelling Journey A Conversation With People About Mattress Double
20 Years Ago double mattress, pena-ernstsen.mdwrite.net,
mexican online pharmacies prescription drugs: Mexican Pharmacy Online – best online pharmacies in mexico
mexican border pharmacies shipping to usa: cheapest mexico drugs – best online pharmacies in mexico
https://mexicoph24.life/# mexican border pharmacies shipping to usa
What Is Glass Repair Leeds And How To Use What Is Glass Repair Leeds And How
To Use upvc Door Repairs meanwood
canadian online pharmacy Licensed Canadian Pharmacy trustworthy canadian pharmacy
The Hidden Secrets Of Does Amazon Ship To Uk Hands-Free Kitchen Faucet Chrome
canadian online drugs canadian pharmacies legit canadian pharmacy online
pharmacy com canada canadian pharmacies online canadian pharmacy
buying prescription drugs in mexico online mexican pharmacy mexican mail order pharmacies
20 Fun Informational Facts About Search Engine Optimization local search engine Optimization services
Why You Should Not Think About Making Improvements To
Your Double Glazing Spares Near Me Double glazing units replacement
best mail order pharmacy canada Licensed Canadian Pharmacy canadian drugstore online
The Locksmiths Automotive Case Study You’ll Never Forget automobile locksmith near my Location
mexico pharmacy: cheapest mexico drugs – pharmacies in mexico that ship to usa
What’s The Point Of Nobody Caring About Double Glazed Units Near Me double-Glazing
indian pharmacy http://indiaph24.store/# india pharmacy mail order
buy medicines online in india
Learn About Best Washing Machines 9kg While Working
From At Home Energy