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
amoxicillin over the counter in canada: amoxil – generic amoxicillin cost
Hi there it’s me, I am also visiting this website on a regular basis, this
website is genuinely pleasant and the users are really sharing fastidious thoughts.
If you desire to take a good deal from this piece of
writing then you have to apply such techniques to your won weblog.
Аттестат школы купить официально с упрощенным обучением в Москве
average cost of prednisone: ray pharm – prednisone tablets canada
Купить диплом о среднем образовании в Москве и любом другом городе
Удивительно, но купить диплом кандидата наук оказалось не так сложно
Легальная покупка диплома о среднем образовании в Москве и регионах
кухни от производителя — Кухонные гарнитуры напрямую от производителя по выгодным ценам.
4 Dirty Little Secrets About The All Crypto Casinos Industry
Crypto casino games
Medicament information. Effects of Drug Abuse.
buying cheap zithromax for sale
Actual about drug. Get information here.
dapoxetine online priligy maxpharm cheap priligy
Узнайте, как приобрести диплом о высшем образовании без рисков
14 Businesses Doing An Amazing Job At Mesothelioma Attorney Mesothelioma Attorneys
can you buy clomid without prescription: generic clomid – can i purchase cheap clomid without rx
Five Killer Quora Answers To Indoor Pellet Stove indoor pellet stove – Margarito,
prednisone 20 mg tablet price: order Prednisone – otc prednisone cream
where can i buy cheap clomid without a prescription generic clomid where to get clomid
buy dapoxetine online: dapoxetine price – priligy maxpharm
amoxicillin 500mg without prescription: amoxil – amoxicillin 500mg capsules
My page … Highstakespoker
Quality articles or reviews is the important to be a focus for the visitors to pay a visit the web page,
that’s what this web site is providing.
Тут можно преобрести купить сейф под ружье купить сейф для пистолета
Тут можно преобрести купить оружейный шкаф заказать оружейный сейф
buying cheap clomid without insurance clomid purchase online rex pharm where to get clomid prices
Диплом пту купить официально с упрощенным обучением в Москве
Your method of explaining all in this article is in fact good, every one can without difficulty be aware of it, Thanks
a lot https://www.myozen.ca/en/produit/orthotherapy/
Nice blog! Is your theme custom made or did you download it from somewhere?
A theme like yours with a few simple adjustements would really make my blog shine.
Please let me know where you got your design. With thanks
how to get cheap clomid prices: cheap clomid – can i order generic clomid no prescription
Actually when someone doesn’t know then its up to other users that they will assist, so
here it takes place.