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
11 “Faux Pas” That Are Actually Okay To Create Using Your
Repair Double Glazing Window Window Repair
Excellent website you have here but I was wondering if you knew
of any user discussion forums that cover the same topics discussed in this article?
I’d really like to be a part of community where I can get comments from other
experienced people that share the same interest. If you have any suggestions,
please let me know. Bless you!
What’s The Good And Bad About Tommy Hilfiger Bag Tommy Hilfiger hand bags; Mohammad,
https://mexicoph24.life/# medication from mexico pharmacy
best online canadian pharmacy canadian pharmacies canadian family pharmacy
Wisdom On Electric Wall Fireplace From An Older Five-Year-Old Electric fireplace wall
(marvelvsdc.faith)
The 10 Scariest Things About Double Glazing Near Me Double Glazing Near Me
The Largest Issue That Comes With Electric Mobility
Scooters Near Me, And How You Can Solve It mobility
scooter Near me for sale (extension.unimagdalena.edu.co)
reputable indian online pharmacy Cheapest online pharmacy online pharmacy india
reputable indian pharmacies https://indiaph24.store/# п»їlegitimate online pharmacies india
indian pharmacy online
Concert Attire Stamford
360 Fairfield Ave,
Stamford, CT 06902, United Ѕtates
+12033298603
Stage Ꮮeft (List.Ly)
15 Terms That Everyone Involved In Double Bunk Beds Top And Bottom Industry Should Know full over full bunk beds (Rolando)
What Is What Is Tier 2 Backlinks? History Of What Is
Tier 2 Backlinks package
What Are The Biggest “Myths” About General Psychiatric Assessment Could Be A Lie Adhd
Psychiatric Assessment (http://Www.40Billion.Com)
Online medicine order http://indiaph24.store/# world pharmacy india
best online pharmacy india
canadian pharmacy uk delivery Licensed Canadian Pharmacy canadian drugs
https://indiaph24.store/# indianpharmacy com
Hello friends, its wonderful piece of writing about tutoringand completely explained, keep it up all the time.
https://mexicoph24.life/# best online pharmacies in mexico
world pharmacy india indian pharmacy fast delivery indianpharmacy com
http://canadaph24.pro/# canadian pharmacy 1 internet online drugstore
https://mexicoph24.life/# reputable mexican pharmacies online
One Key Trick Everybody Should Know The One Tommy Hilfiger Cross Bag
Trick Every Person Should Be Able To Tommy hilfiger Bags Sale
Sell Products At Home Tips From The Most Successful In The Industry who
sell avon near me (vacsatc.Hu)
15 Rabbit Vibrator Online Benefits Everybody Must Know rabbit Vibrators
best online pharmacies in mexico Online Pharmacies in Mexico best online pharmacies in mexico
What’s The Current Job Market For Boat Accident Attorney Professionals Like?
boat Accident attorney
indianpharmacy com world pharmacy india india pharmacy
20 Best Tweets Of All Time Replacement Windows Leeds Upvc Door Repair Roundhay
7 Simple Tricks To Moving Your Best price Beko washing machines;
bookmarkzones.trade,