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
See What Mesothelioma Litigation Tricks The Celebs Are Using mesothelioma litigation
tamoxifen and osteoporosis: cost of tamoxifen – tamoxifen and bone density
nolvadex vs clomid how to prevent hair loss while on tamoxifen tamoxifen dosage
lisinopril 10 mg brand name in india: generic zestril – 50mg lisinopril
buy cytotec over the counter order cytotec online Cytotec 200mcg price
http://nolvadex.life/# who should take tamoxifen
cipro buy cipro online ciprofloxacin over the counter
buy cipro: cipro pharmacy – antibiotics cipro
http://cytotec.club/# buy cytotec online
buy ciprofloxacin over the counter ciprofloxacin mail online antibiotics cipro
Side By Side Fridge Freezer Under Counter: What No One Is Talking
About which brand has the best side by side Refrigerator
cytotec buy online usa: cytotec pills buy online – buy misoprostol over the counter
http://nolvadex.life/# effexor and tamoxifen
cheap propecia without rx buy generic propecia without a prescription propecia generics
price of zestril 30 mg lisinopril pills lisinopril 20 mg buy
lisinopril 10 mg tablet price: lisinopril 419 – lisinopril online pharmacy
Hi, i read your blog occasionally and i own a similar one and
i was just curious if you get a lot of spam remarks?
If so how do you protect against it, any plugin or anything you can recommend?
I get so much lately it’s driving me mad so any assistance is very much appreciated.
where can i buy cipro online cipro ciprofloxacin buy cipro cheap
http://lisinopril.network/# zestril drug
tamoxifen and osteoporosis tamoxifen buy tamoxifen and bone density
Thɑnk you for the good writeup. It in truth was a leisure account it.
Look advanced too more Ьrought agreeable from you!
However, how could we keep in tоucһ?
Feel free to visit my weƅ-site: jamuslot
You’ll Be Unable To Guess Dreme L30’s Benefits dreme l30 (http://www.tanzlife.co.Tz)
nolvadex steroids tamoxifen for gynecomastia reviews cost of tamoxifen
cytotec abortion pill: Misoprostol 200 mg buy online – Misoprostol 200 mg buy online
buy cytotec over the counter: cytotec pills buy online – buy cytotec over the counter
A Step-By’-Step Guide For Leeds Door And Window upvc Door Repairs Roundhay
Five Killer Quora Answers On Robot Vacuum Empty Itself Robot Vacuum Empty Itself
Why Mobility Scooter For Sale Is The Best Choice For You?
electric mobility scooters for Sale cheap
http://cytotec.club/# buy cytotec over the counter
Robot Vacuum Best Explained In Fewer Than 140 Characters robot mop and Vacuum combo