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
Check out my webpage; Daycare Near Me – Find The Best Daycares Near You
I read this post completely regarding the difference of most recent and
preceding technologies, it’s amazing article.
med pharmacy: online pharmacy no prescription needed lortab – asda pharmacy cialis
thriftway pharmacy store locator: bupropion target pharmacy – pharmacies shipping to usa
Five Things Everyone Makes Up On The Subject Of Auto Folding Mobility Scooters Uk
automatic folding mobility scooter (Francis)
Think You’re Ready To Start Doing 10kg Washing Machine
For Sale? Try This Quiz http://www.023456789.xyz
Полезная информация как купить диплом о высшем образовании без рисков
trust pharmacy online reviews: online pharmacy clomid – discount pharmacy viagra
Nine Things That Your Parent Taught You About Living
Room Couch Sets couch set, Gabriela,
10 Life Lessons That We Can Learn From Fridge Freezers For Sale Cheapest Fridge Freezers Uk
ADHD Medication Pregnancy: The Good, The Bad, And The Ugly can a general practitioner prescribe adhd medication
percocet overseas pharmacy: kamagra india pharmacy – mexican viagra pharmacy
15 Things You Don’t Know About Car Door Unlock Service Car
Door Lockout (Olderworkers.Com.Au)
veterinary online pharmacy: rx us meds pharmacy – vons pharmacy
10 Best Books On Treadmill With Incline Of 12 treadmills that incline; https://spadenovel3.werite.net/,
The 10 Most Scariest Things About Add In Adult Women add in adult women