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
https://ozempic.art/# ozempic
I know this web site provides quality dependent content and additional information, is there any other site which presents these
information in quality?
В магазине сейфов предлагают сейфы купить в москве сейфы москва
The Reasons Folding Transport Wheelchair In 2023 Is The Main Focus Of All People’s Attention.
2023 wheelchair lightweight folding
Why Assessment For Mental Health Is Your Next Big Obsession mental status assessment,Daniele,
I’m curious to find out what blog system you’re utilizing?
I’m having some minor security problems with my latest
website and I’d like to find something more secure.
Do you have any solutions?
Hello there! Do you know if they make any plugins to assist with SEO?
I’m trying to get my blog to rank for some targeted
keywords but I’m not seeing very good success. If you know of any please share.
Thank you!
15 Gifts For Your Mental Assessment Lover In Your Life psychiatrist Mental health assessment
cheapest rybelsus pills: semaglutide online – rybelsus price
В магазине сейфов предлагают сейф цена купить сейф цена москва
Cheers! A good amount of facts!
Here is my website; https://mostbet-bk.pl
See What Demist Double Glazing Near Me Tricks The Celebs Are
Utilizing demist double glazing – Maricruz,
вызов нарколога цена вызов нарколога цена .
Thanks for sharing your info. I truly appreciate your efforts and I will be waiting for your next post thanks once again.
Where Can You Find The Top Treadmills With Incline Information? Small treadmill incline
Also visit my site: Daycares By Category
cheapest rybelsus pills: semaglutide online – rybelsus pill
вывод из запоя круглосуточно ростов вывод из запоя круглосуточно ростов .
Hi there, I found your site by the use of Google while searching
for a comparable matter, your site came up, it
appears to be like great. I have bookmarked it in my google bookmarks.
Hello there, just turned into alert to your weblog thru Google, and found that it is truly informative.
I’m gonna watch out for brussels. I will appreciate when you continue this in future.
A lot of folks might be benefited from your writing.
Cheers!
https://rybelsus.shop/# rybelsus pill
The 9 Things Your Parents Taught You About Comfortable Couches For Sale Comfortable Couches For Sale
ozempic coupon: ozempic cost – buy ozempic
How To Save Money On Sectional Couches Sectional couches with Recliners
Профессиональный сервисный центр по ремонту электросамокатов в Москве.
Мы предлагаем: тюнинг электросамокатов в москве
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
How To Outsmart Your Boss On Upvc Door Lock Replacement replacing a Upvc door panel
cheapest rybelsus pills semaglutide tablets rybelsus coupon
The 10 Scariest Things About Window Glass Replacement window Glass Replacement
5 Clarifications On Electric Fires Wall Mounted electric fireplace hang on wall, Miriam,
http://rybelsus.shop/# semaglutide tablets
It’s really a great and useful piece of info. I am glad that
you shared this useful information with us. Please keep us up to date like
this. Thank you for sharing.