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
Hello very cool blog!! Guy .. Beautiful .. Superb .. I will bookmark your website and take the feeds also?
I am happy to seek out a lot of useful information here in the submit, we need develop more
strategies in this regard, thank you for sharing.
. . . . . http://gaziantep.ogo.org.tr/question/meubles-en-coin-de-cuisine-optimisation-de-lespace-et-esthetique-7/
1 vin http://1win7004.ru/ .
google play купить отзывы google play купить отзывы .
Warm XXX black ladies having sex, damn warm!.
google play отзывы google play отзывы .
Wonderful site you have here but I was curious about if you
knew of any discussion boards that cover the same topics talked about in this article?
I’d really love to be a part of online community where I can get responses from other experienced individuals that share
the same interest. If you have any recommendations, please
let me know. Thanks!
Wow! This blog looks just like my old one! It’s on a completely different subject but it has pretty much the same
layout and design. Superb choice of colors!
That is a great tip especially to those new to the blogosphere.
Short but very precise info… Appreciate your sharing this
one. A must read post!
мостбет скачать андроид http://mostbet5003.ru/ .
http://usaindiapharm.com/# USA India Pharm
https://vintageamericanapodcast.com/ – Discover America’s vintage heritage through engaging storytelling
Warm and balmy sex adventures in India? Yes please!
UsaIndiaPharm USA India Pharm UsaIndiaPharm
UsaIndiaPharm: USA India Pharm – USA India Pharm
Very warm! Bollywood actress XXX is on fire!.
When someone writes an paragraph he/she keeps the image of a user in his/her brain that how a user
can know it. So that’s why this piece of writing is perfect.
Thanks!
Drugs prescribing information. Effects of Drug Abuse.
mirtazapine 15 mg tablet tev
Actual trends of drugs. Read information now.
levaquin 500 mg side effects
Terima kasih atas informasinya yang sangat bermanfaat.
Kebetulan saya juga menemukan informasi menarik di ultra4dtoto yang mungkin bisa melengkapi pembahasan ini.
повышение рейтинга криптовалюты повышение рейтинга криптовалюты .
Hot BF? A Lot More like Warm Mess! (bf attractive warm).
https://usaindiapharm.com/# indian pharmacy
продвижение крипты продвижение крипты .
один вин 1win7016.ru .
Do you mind if I quote a couple of your articles as long as I provide credit and sources
back to your webpage? My blog site is in the exact same area of interest
as yours and my visitors would truly benefit from a lot of the information you provide here.
Please let me know if this alright with you. Cheers!
1win.kg 1win.kg .
https://usaindiapharm.shop/# Online medicine order
https://adaptable-goat-dd3cmf.mystrikingly.com/blog/54f91bd122f
Hot scene, warm scene! Gay BDSM porn at its finest.
USA India Pharm: USA India Pharm – pharmacy website india