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
Keep on working, great job! cursilloscolombia.org
Attractive section of content. I just stumbled upon your blog and in accession capital
to assert that I acquire actually enjoyed account your blog posts.
Anyway I will be subscribing to your feeds and even I achievement
you access consistently quickly.
en iyi slot siteler: slot siteleri 2024 – slot siteleri bonus veren
http://slotsiteleri.bid/# deneme bonusu veren siteler
casino slot siteleri: deneme bonusu veren slot siteleri – canl? slot siteleri
Hi there would you mind letting me know which
webhost you’re working with? I’ve loaded your blog in 3 completely different internet browsers
and I must say this blog loads a lot faster then most.
Can you recommend a good internet hosting provider at a fair price?
Many thanks, I appreciate it!
Hi! Do you want to get 0.001 BTC for free????
I found an opportunity to get them with the help of Mirax casino (find
it in the search engine), you need to register there and when registering enter the bonus code CHIPMX, the account will be credited bonus 0,0001 BTC which can be withdrawn if you
make X10, I did it and I withdrew, try and you!
yeni slot siteleri: canl? slot siteleri – slot casino siteleri
en iyi slot siteleri 2024: slot bahis siteleri – yeni slot siteleri
It’s very simple to find out any matter on web as
compared to books, as I found this paragraph at this web page.
https://sweetbonanza.network/# sweet bonanza taktik
slot oyunlar? siteleri: bonus veren casino slot siteleri – slot siteleri 2024
The Most Underrated Companies To In The Hyundai Spare Key Industry
How Much Does A Hyundai Key Fob Cost
http://denemebonusuverensiteler.win/# bahis siteleri
Guide To Queen Size Pull Out Couch: The Intermediate Guide In Queen Size Pull Out Couch queen size pull out couch – Darlene,
I’m not sure exactly why but this web site is loading extremely slow for me.
Is anyone else having this problem or is it a issue on my end?
I’ll check back later and see if the problem still exists.
Нellⲟ there! This is my first visit t᧐ yoսr blog!
We are a team of volunteers ɑnd starting a new project
іn a community in tһe same niche. Your blog рrovided ᥙs usеful information tο work on.
You have ɗone a wonderful job!
Feel free tо surf to my blog PokemonToto Alternatif
Appreciate it, Loads of facts!
Профессиональный сервисный центр по ремонту планетов в том числе Apple iPad.
Мы предлагаем: профессиональный ремонт ipad
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
Drug information for patients. Drug Class.
generic pregabalin vs lyrica
Some about medication. Get information now.
https://denemebonusuverensiteler.win/# bahis siteleri
Официальная покупка диплома вуза с упрощенной программой обучения
Профессиональный сервисный центр по ремонту планетов в том числе Apple iPad.
Мы предлагаем: айпад сервисный центр
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
http://denemebonusuverensiteler.win/# deneme bonusu veren siteler
Профессиональный сервисный центр по ремонту планетов в том числе Apple iPad.
Мы предлагаем: ремонт ipad москва
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
10 Inspirational Graphics About Nissan Key Replacement Prices nissan Qashqai Key replacement uk
http://denemebonusuverensiteler.win/# bahis siteleri
this is not exclusively the https://cheapairportparkingjfk.com/, but and save you need a lot time. another option that will save the customer from a huge amount of troubles, is to ask about this relative or a friend.
The 9 Things Your Parents Taught You About Replacement
Smart Car Key replacement smart car key (45.4.175.178)
sapporo 88