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
Drug information. Short-Term Effects.
can i purchase azathioprine price
All information about drug. Read information now.
buy prednisone without prescription paypal generic Prednisone prednisone 20 mg tablets
This is a topic that’s neаr tⲟ mmy heart…
Takе care! Wһere aгe үoᥙr contact details though?
Мy web blog Laskar303
Guide To Treadmill Fold Up: The Intermediate
Guide For Treadmill Fold Up Treadmill Fold Up
cheap priligy max pharm priligy max pharm
25 Amazing Facts About Free Pragmatic 프라그마틱 순위
врач нарколог на дом платный https://motik13.0pk.me/viewtopic.php?id=1995 .
Wymagania Prawne Na Posiadanie Licencji Jazdy Na Traktor Z Przyczepą
Prawo Jazdy Na Traktor Z Przyczepą: Wszystko, Co Musisz Wiedzieć Prawo Jazdy na traktor
Hey There. I found your blog using msn. This is an extremely well written article.
I will make sure to bookmark it and come back to read more of your useful info.
Thanks for the post. I’ll certainly comeback.
Узнай все о лапароскопия варикоцеле признаки варикоцеле
tor markets links tor market darkmarkets
Диплом техникума купить официально с упрощенным обучением в Москве
betterbodies2.store/kupit-attestat-starogo-obraztsa-v-orenburge.html
GasDank is your go-to for ordering weed online in Canada. We are a safe and discreet marijuana mail order service based in Toronto, Ontario, also offering same-day delivery throughout the GTA https://a2s.in/redirect-to/?redirect=https://gasdank.com/store-locator/
cost of amoxicillin 875 mg: amoxil com pharm – amoxicillin from canada
Всем привет! Может кто знает, где почитатьразные статьи о недвижимости? Сейчас читаю – https://avgust-shop.ru
This is a topic that is near to my heart… Best wishes!
Exactly where are your contact details though?
Kudos, Helpful information.
Всем привет! Может кто знает, где почитатьразные блоги о недвижимости? Пока нашел – https://avgust-shop.ru
врач нарколог на дом платный http://www.motik13.0pk.me/viewtopic.php?id=1995/ .
I’m not that much of a online reader to be honest but your sites really nice, keep it up!
I’ll go ahead and bookmark your site to come back later. Many thanks
prednisone 20mg for sale: cheap prednisone – prednisone 10
The Top Reasons Why People Succeed In The Car Accident Case Industry
attorney car accident near me [Bridgett]
платный нарколог на дом http://zavitai.mybb.social/viewtopic.php?id=89/ .
It is not my first time to pay a quick visit this
web page, i am browsing this site dailly and get good data
from here all the time.
Приветствую. Может кто знает, где найтиполезные блоги о недвижимости? Сейчас читаю – https://avgust-shop.ru
buy cheap amoxicillin online: Com Pharm – can you buy amoxicillin over the counter in canada
нарколог на дом в краснодаре https://flanrp.rolevaya.com/viewtopic.php?id=146 .
fortune mouse gratis
fortune mouse gratis слот
amoxicillin 500 mg tablets: Amoxicillin buy online – medicine amoxicillin 500
15 Amazing Facts About Mesothelioma Asbestos Lung Cancer Mesothelioma lawyer