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
does tamoxifen make you tired: tamoxifen alternatives – tamoxifen rash pictures
http://lisinopril.network/# order lisinopril online united states
casino con bono de bienvenida sin deposito http://sin-deposito-peru.org/ .
Technology Is Making Will CSGO Cases Go Up In Price Better
Or Worse? case skins
Where Will Treadmill Desk Uk Be One Year From This Year?
Treadmill Under Desk
Having read this I believed it was really informative.
I appreciate you finding the time and effort to put this content together.
I once again find myself spending a lot of time both
reading and posting comments. But so what, it was still worthwhile!
15 Things You Didn’t Know About Misted Double Glazing Repairs Near Me double glazed Units
Hello friends, fastidious article and pleasant arguments commented here, I am in fact enjoying
by these.
10 Places To Find Double Glaze Repair Near Me Repairs Double Glazed Windows
You’ll Never Guess This Cheap Mobility Scooters Near
Me’s Secrets cheap mobility scooters near me
http://cytotec.club/# buy cytotec pills online cheap
order cheap propecia without insurance: order generic propecia without insurance – cost of cheap propecia
Buzzwords, De-Buzzed: 10 Other Methods For Saying Window Repairman Window repairs
casino con bono de bienvenida sin deposito http://www.sin-deposito-peru.org/ .
https://cytotec.club/# buy cytotec in usa
See What Compact Treadmill Incline Tricks The Celebs Are Making Use Of Compact Treadmill Incline
cipro 500mg best prices: ciprofloxacin order online – ciprofloxacin
purchase cytotec: Abortion pills online – buy cytotec over the counter
buying generic propecia for sale: propecia tablet – cost of cheap propecia without dr prescription
10 Undisputed Reasons People Hate Volkswagen Keys volkswagen locked keys in car
https://cytotec.club/# Misoprostol 200 mg buy online
You’ll Be Unable To Guess Car Key Battery Replacement’s Tricks Car Key Battery Replacement
Топовая Букмекерская контора – 1win
15 Mental Health Tests NHS Benefits That Everyone Should Be Able
To online Mental health assessments
Why We Love Best Price beko washing machines Prices Washing Machines (And You Should Also!)
This History Behind Licensed Slots Will Haunt You
For The Rest Of Your Life! Themed Slot Games
Hi there, the whole thing is going well here and ofcourse every one is sharing data,
that’s in fact excellent, keep up writing.
cost cheap propecia without dr prescription: cost generic propecia for sale – buying propecia for sale
http://lisinopril.network/# lisinopril 10mg
http://nolvadex.life/# tamoxifen lawsuit