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
What’s The Fuss About Electric Fireplace Freestanding?
freestanding fireplace – Nam,
Грузовые шины КАМАЗ вездеход являются одним из самых востребованных видов шин для грузовых автомобилей. Грязевые шины для внедорожников. Все размеры резина aeolus
Diosa introduced me to the CrashPad. I was drawn to the revolutionary act of
queer/trans POC making queer porn for different queer
and trans POC.
https://ozempic.art/# Ozempic without insurance
The Unknown Benefits Of Toto Sites 토토사이트 모음
ozempic coupon: ozempic coupon – ozempic online
Отечественный интернет-магазин “Мебель-шмммммммебель” приглашает широченный круг мебели для кухонь. ЯЗЫК нас вы выкопаете все нужное для тварей уютного и многофункционального внутреннего убранства http://www.tyfapao6kuhnishki.ru/.
Wonderful site. Plenty of helpful info here. I am sending it
to several buddies ans also sharing in delicious.
And naturally, thank you to your effort!
I am sure this post has touched all the internet
viewers, its really really fastidious piece of writing on building
up new website.
Официальный сайт казино Криптобосс:
ваша удача на расстоянии
клика онлайн казино криптовалют
Как зайти на Криптобосс через зеркало cryptoboss casino официальный сайт
ozempic coupon Ozempic without insurance Ozempic without insurance
Профессиональный сервисный центр по ремонту автомагнитол в Москве.
Мы предлагаем: ремонт автомагнитол рядом
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
9 Lessons Your Parents Taught You About Bean Coffee Maker Bean Coffee Maker
Porn 720
ozempic coupon: ozempic coupon – buy cheap ozempic
rybelsus coupon: semaglutide tablets – cheapest rybelsus pills
30 Inspirational Quotes About Togel4d toto4D
Hi there just wanted to give you a quick heads up. The text in your post seem to be running off the screen in Internet
explorer. I’m not sure if this is a formatting
issue or something to do with browser compatibility but I thought I’d post to let you know.
The design look great though! Hope you get the issue resolved soon.
Many thanks
Heya i’m for the first time here. I found this board and I
find It really useful & it helped me out a lot.
I am hoping to present one thing back and help others like you helped me.
http://rybelsus.shop/# rybelsus cost
ozempic generic ozempic cost buy ozempic pills online
https://rybelsus.shop/# semaglutide online
ozempic coupon buy ozempic ozempic generic
https://ozempic.art/# ozempic
I do believe all of the ideas you’ve presented to your post.
They are really convincing and will definitely work.
Nonetheless, the posts are too short for novices.
May you please lengthen them a little from subsequent time?
Thank you for the post.
10 Places That You Can Find Replacing Lost Car Keys Ford ford kuga key fob replacement (Audrea)
rybelsus pill semaglutide cost buy rybelsus online
https://rybelsus.shop/# semaglutide online
buy cheap ozempic buy ozempic pills online ozempic generic