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
Ten Replacement Ferrari Key Uk That Will Actually Change Your Life Cars
buy rybelsus online semaglutide cost buy semaglutide online
Познакомьтесь капля нашим проф коллективом, яже создаёт чудесную этажерка, воплощая ваши мечты в течение реальность https://cehitae2kuhnishki.ru/.
Профессиональный сервисный центр по ремонту автомагнитол в Москве.
Мы предлагаем: отремонтировать автомагнитолу
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
https://ozempic.art/# buy cheap ozempic
Профессиональный сервисный центр по ремонту автомагнитол в Москве.
Мы предлагаем: сервисные центры по ремонт автомагнитол
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
If some one needs expert view regarding blogging then i recommend him/her to pay
a visit this blog, Keep up the nice work.
Also visit my blog Top KL caterer
5 Killer Quora Answers On Toto4d toto4d (link-togel-resmi65666.blogstival.com)
You actually make it seem so easy with your presentation but I find this topic
to be really something which I think I would
never understand. It seems too complex and extremely broad for me.
I’m looking forward for your next post, I’ll try to get
the hang of it!
For hottest news you have to visit internet and on the web
I found this website as a best site for most up-to-date updates.
Hurrah! After all I got a website from where I know how
to genuinely take useful data regarding my study and knowledge.
Сервисный центр предлагает ремонт кондиционера ecostar недорого ремонт кондиционера ecostar на дому
http://rybelsus.shop/# buy semaglutide pills
Как улучшить настроение другу? Посмотрите
смешные анекдоты и поделитесь с близкими.
Great post.
https://rybelsus.shop/# buy semaglutide online
rybelsus coupon: cheapest rybelsus pills – cheapest rybelsus pills
This Story Behind French Door Fridge Best Will Haunt You
Forever! which french door fridge is the best [https://www.Google.gr/url?q=https://cain-murdock-2.federatedjournals.com/the-biggest-problem-with-french-style-fridge-and-what-you-can-do-to-fix-it]
Сервисный центр предлагает ремонт кофемашин centek недорого отремонтировать кофемашины centek
A Mortgage Loan Modification Can Stop A Judicial Foreclosure Case
신용불량자 대출
https://ozempic.art/# ozempic coupon
Bunk Beds With Single And Double Tools To Help You Manage
Your Everyday Lifethe Only Bunk Beds With Single And
Double Trick Every Individual Should Be Able To
bunk beds With single and double
14 Questions You Shouldn’t Be Afraid To Ask About Upvc Door Repairs Near
Me emergency Upvc door repairs near Me (clashofcryptos.trade)
Hey! Do you know if they make any plugins to assist
with Search Engine Optimization? I’m trying to get my
blog to rank for some targeted keywords but I’m not seeing very
good results. If you know of any please share. Appreciate it!
After I initially left a comment I seem to have clicked on the -Notify me when new comments are added- checkbox and from now on whenever a comment is added I receive four emails with the same comment.
Perhaps there is a way you can remove me from that service?
Thanks!
Feel free to surf to my web page – Pinterest
ozempic cost ozempic generic buy ozempic
buy cheap ozempic: Ozempic without insurance – ozempic coupon
Neat blog! Is your theme custom made or did
you download it from somewhere? A theme like yours with a few simple
tweeks would really make my blog jump out. Please let me know where you got your design. Thanks
Профессиональный сервисный центр по ремонту электросамокатов в Москве.
Мы предлагаем: ремонт электросамокатов на дому с выездом мастера
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
rybelsus pill: cheapest rybelsus pills – cheapest rybelsus pills