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
The intelligence is the most effective completely dry herb vape I have actually attempted.
Here is my blog post where to buy plant based meals
Existing legislation covers 3 legal process.
Here is my blog Bookmarks
Pretty nice post. I just stumbled upon your weblog and
wished to say that I’ve truly enjoyed surfing around your blog
posts. In any case I’ll be subscribing to your feed and I hope you write again soon!
They have products to market or services to provide.
Also visit my blog … Bookmarks
снятие наркотической ломки http://www.snyatie-lomki-narkolog11.ru .
XYGENYX, a licensing company for FDA-registered items.
Feel free to surf to my web site – ranch wedding venues houston texas
What is New U Life SOMADERM Transdermal Gel.
Here is my blog :: Bookmarks
pharmacy in artane castle: best online pharmacy cialis – target finasteride pharmacy
Securely pay to hire your attorney.
Also visit my homepage – bankruptcy lawyers texas
The larger Slim battery, terrific for longer runtimes.
Here is my website max protect reviews
order xenical online pharmacy: homepage – priligy malaysia pharmacy
Sahabet Casino’da yeni oyuncular, en buyuk hos geldin bonuslar?n? alarak oyun keyfini c?karabilir. En sevilen slotlar Sahabet’te en iyi odullerle dolu. Bu f?rsat? kac?rmay?n ve 500% bonus elde edin. Sahabet, yeni y?lda kazand?ran kumarhane olarak dikkat cekiyor.
USP human growth hormonal agent (somatropin).
Feel free to visit my web page :: foreign auto repair shop
The bigger Slim battery, great for longer runtimes.
Here is my website: dual catch can setup
WTR Дизайн человека https://raschet-karty-dizayn-cheloveka.ru Дизайн человека. 4/6 Дизайн человека.
срочная наркологическая помощь срочная наркологическая помощь .
reliable rx pharmacy reviews: cheap online pharmacy – malaysia online pharmacy store
They have products to market or services to provide.
Here is my blog: replacement window cost 2023
You have made some good points there. I checked on the net for more information about the issue and found most individuals will go along
with your views on this website.
comprar finpecia en pharmacy2home: rite aid pharmacy store closings – online pharmacy prozac no prescription
однодневный ретрит https://ретриты.рф
advanced care rx pharmacy: discount pharmacy – uk pharmacy
What Is Ford Fiesta Replacement Key Cost Uk And How To Utilize It ford key battery replacement
Unlike other contractors, we value your time.
my site … newulife hgh gel scam
XYGENYX, a licensing company for FDA-registered products.
Look at my homepage – Bookmarks
oakdell pharmacy sunday store hours: provigil indian pharmacy – tour de pharmacy online
In this situation, the life insurance business’s loan.
Here is my website … Bookmarks
bm pharmacy viagra: uk pharmacy online – the people’s pharmacy
Если вы искали где отремонтировать сломаную технику, обратите внимание – ремонт бытовой техники
A baby leg twitches when sleeping can have as several as
100 convulsions a day.