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
Personal chapter 11 bankruptcy vs chapter 13
is essentially a credentials procedure.
testosterone cypionate online pharmacy: lorazepam indian pharmacy – azithromycin online pharmacy no prescription
The larger Slim battery, excellent for longer runtimes.
Check out my web-site :: gen2 foil drive assist max
XYGENYX, a licensing firm for FDA-registered products.
Here is my webpage; texas ranch wedding venues
cytotec online pharmacy: viagra generic pharmacy – global pharmacy viagra
In this situation, the life insurance policy business’s cash.
Also visit my web page – Bookmarks
Let me reveal you in my evaluation of NewULife.
Also visit my page – Bookmarks
A child can have as numerous as 100 convulsions
a day.
Also visit my webpage :: at what age do infantile spasms start
Полезные советы по предотвращению дубликатов номеров, как не допустить дублирования номеров при работе с документами, лучшие методы избежания дублирования номеров. Продвинутые методы предотвращения дубликатов номеров, лучшие практики по устранению дубликатов номеров. Как избежать дубликатов номеров в офисе, шифрование номеров: как избежать дубликатов. лучшие способы избежания дубликатов номеров, как предотвратить дублирующиеся номера на документах.
дубликаты номеров дубликаты номеров .
india online pharmacy: phuket pharmacy viagra – viagra in indian pharmacy
Portable vaporizers make use of batteries as a
power source.
My blog: plant based vape uk
QMR Дизайн человека https://humandesignplanet.ru Дизайн человека. 1/3 Дизайн человека.
Let me reveal you in my evaluation of NewULife.
my blog: custom vinyl windows albuquerque
USP human growth hormonal agent (somatropin).
Here is my site – Bookmarks
The intelligence is the best dry herb vape I have attempted.
Feel free to visit my homepage; plant based meat alternatives uk
5 Killer Quora Answers On Gorenje Fridge Freezer Retro fridge freezer Retro
mexican online pharmacy percocet: ciprofloxacin pharmacy – flovent online pharmacy
In this situation, the life insurance policy business’s loan.
Feel free to visit my web blog Bookmarks
A child can have as numerous as 100 convulsions a day.
Review my web blog: do infantile spasms happen while sleeping
cialis online from us pharmacy: metoprolol people’s pharmacy – online pharmacy greece
pharmacy assistant certification online: bactrim pharmacy – no prescription viagra online pharmacy
These are actually great ideas in on the topic of blogging.
You have touched some pleasant factors here. Any way keep up wrinting.
капельница от запоя на дому капельница от запоя на дому .
Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically
tweet my newest twitter updates. I’ve been looking for a plug-in like this for quite some
time and was hoping maybe you would have some experience with something
like this. Please let me know if you run into anything.
I truly enjoy reading your blog and I look forward to
your new updates.
What is New U Life SOMADERM Transdermal Gel.
Here is my page; european auto repair shop near me
I’m gone to tell my little brother, that he should also pay a quick
visit this website on regular basis to obtain updated from newest
news.
What a information of un-ambiguity and preserveness of valuable know-how regarding unexpected feelings.
What is New U Life SOMADERM Transdermal Gel.
Also visit my web page – ceramic pot painting near me
Hi, this weekend is nice designed for me, for the reason that this moment i am reading this enormous informative paragraph here at my residence.
Если вы искали где отремонтировать сломаную технику, обратите внимание – профи ремонт