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 Save Money On American Style Fridge Freezer Freestanding fridge freezer
The Most Common Mistakes People Do With Volvo V70 Key Volvo vnl key
Viagra without a doctor prescription Canada Cheapest place to buy Viagra sildenafil online
I like the helpful information you provide in your articles. I?ll bookmark your weblog and check again here frequently. I am quite sure I will learn many new stuff right here! Good luck for the next!
5 Killer Quora Answers To Double Glazing Repairs Leeds double glazing Repairs leeds
Cheap Viagra 100mg Generic Viagra online buy viagra here
The Double Glazing Window Repairs Awards: The Most Stunning, Funniest, And The Most Bizarre Things We’ve Seen Double glazed Window repairs near me
Kamagra tablets: super kamagra – Kamagra tablets
http://cenforce.pro/# Cenforce 100mg tablets for sale
You’ll Never Guess This Leeds Windows’s Tricks upvc window repair leeds
https://levitrav.store/# Vardenafil buy online
https://cialist.pro/# Buy Tadalafil 10mg
Purchase Cenforce Online Buy Cenforce 100mg Online Buy Cenforce 100mg Online
Viagra Tablet price: Buy Viagra online cheap – Cheapest Sildenafil online
Cenforce 150 mg online: Buy Cenforce 100mg Online – Cenforce 150 mg online
Cheap Sildenafil 100mg viagras.online generic sildenafil
5 Killer Quora Answers To Standing Desk Treadmill Standing Desk Treadmill
Hi there ѡould you mind letting me know which web
host үoᥙ’гe worқing ᴡith? I’ve loaded ʏour blog іn 3 ϲompletely different
ingernet browsers and I muust sаy this blog
loads a ⅼot fastger tһen most. Can you suggest a
good internet hosting provider at a fair ρrice? Thankѕ a lot, І ɑppreciate it!
Als᧐ visit mү web blog – Slot Pulsa
What’s Holding Back In The Saab Replacement Keys Industry?
Saab 900 key
7 Essential Tips For Making The Most Of Your Online Charity Shop Uk Clothes Ping Pong Racket Kit
Sildenafil 100mg price: Buy generic 100mg Viagra online – Cheap generic Viagra online
п»їkamagra: kamagra pills – Kamagra 100mg
Kamagra 100mg price kamagra pills Kamagra Oral Jelly
15 Shocking Facts About Stoves Defra That You Didn’t Know About defra accredited stoves [Spectr-sb116.ru]
See What Link Daftar Gotogel Tricks The Celebs Are Utilizing Link daftar Gotogel
5 Tools Everyone In The Designer Handbags Large Industry Should Be Making Use Of Designer handbags vintage
Buy Vardenafil 20mg Cheap Levitra online Buy Vardenafil 20mg online
I like the valuable information you provide in your articles. I will bookmark your blog and check again here regularly. I am quite certain I will learn a lot of new stuff right here! Good luck for the next!
http://levitrav.store/# Levitra 10 mg best price
10 Facts About Double Bed Mattress That Will Instantly Put You In An Optimistic
Mood Double mattress extra firm