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
The 10 Most Terrifying Things About Locksmith For Auto Keys
locksmith for auto keys
An Desk Treadmill Success Story You’ll Never Believe walking treadmill with desk
Question: How Much Do You Know About Triple Sleepers?
Single Double Bunks
buying prescription drugs in mexico online medication from mexico pharmacy mexican mail order pharmacies
https://yuppe.ru/
https://ya-chinavisa-1.us-southeast-1.linodeobjects.com/
The following are some things to assume about when selecting between attire.
mexico drug stores pharmacies: medication from mexico pharmacy – mexican border pharmacies shipping to usa
mexico drug stores pharmacies: mexican border pharmacies shipping to usa – medicine in mexico pharmacies
An Intermediate Guide For Auto Accident Compensation Auto Accident law firm
Guide To ADHD Titration: The Intermediate Guide The Steps To
ADHD Titration Adhd Titration
buying from online mexican pharmacy: buying prescription drugs in mexico – п»їbest mexican online pharmacies
5 Lessons You Can Learn From Window Repair Near upvc window repair near me (olive-yucca-hvs8qp.mystrikingly.Com)
pharmacies in mexico that ship to usa: reputable mexican pharmacies online – mexican drugstore online
https://accounting22.b-cdn.net/research/accounting22-(143).html
So, go forward, and let the pure shine of your dress communicate volume for you.
buying prescription drugs in mexico: best online pharmacies in mexico – mexican online pharmacies prescription drugs
8 Tips To Up Your Peugeot Car Key Game Peugeot 107 replacement key Cost
Remarkable issues here. I am very happy to look your article.
Thanks a lot and I’m looking ahead to touch you.
Will you kindly drop me a mail?
buying prescription drugs in mexico: buying prescription drugs in mexico – buying prescription drugs in mexico online
7 Simple Tips For Moving Your Demo Slot Zeus Vs Hades
Gratis rtp Zeus vs hades
15 Latest Trends And Trends In Boat Accident Attorney boat accident attorneys – Evie,
best online pharmacies in mexico: best online pharmacies in mexico – buying prescription drugs in mexico
mexican border pharmacies shipping to usa: buying prescription drugs in mexico – mexican rx online
purple pharmacy mexico price list: mexican rx online – п»їbest mexican online pharmacies
Pills information for patients. Cautions.
can i order generic spiriva pills
Some news about drug. Get here.
Treatment For Anxiety Techniques To Simplify Your Everyday Lifethe Only
Treatment For Anxiety Trick Every Individual Should Be Able
To Treatment for anxiety
ESTUDOS BÍBLICOS PROFUNDOS E PREGAÇÕES INSPIRADORAS
O Jesus Nos Ensina oferece estudos bíblicos profundos, pregações inspiradoras e recursos gratuitos
para te ajudar a crescer em sua fé.
best online pharmacies in mexico: mexico drug stores pharmacies – mexico drug stores pharmacies
mexico drug stores pharmacies best online pharmacies in mexico mexico pharmacies prescription drugs
mexico drug stores pharmacies: п»їbest mexican online pharmacies – mexico pharmacies prescription drugs
7 Easy Tips For Totally Refreshing Your Medication For ADHD And Anxiety
Non Prescription Adhd Medication For Adults