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
Chiropractic treatments have made such a difference in my life! Excited to see what Lakewood Chiropractor offers in Lakewood
deneme bonusu veren siteler yat?r?ms?z deneme bonusu veren siteler deneme bonusu veren siteler
Incredible! This blog looks exactly like my old one!
It’s on a entirely different subject but it has pretty much the
same layout and design. Superb choice of colors!
It’s an amazing paragraph for all the internet visitors; they will obtain advantage from it I am sure.
Signature Drinks 오피사이트 – Vania –
10 Inspirational Graphics About Private ADHD Clinic Private Adhd Assessment Coventry
rubber stamp online maker rubber stamp online maker .
casino bahis siteleri Canl? Casino Siteleri deneme bonusu veren casino siteleri
create stamp online free create stamp online free .
Useful information. Fortunate me I discovered
your site unintentionally, and I’m shocked why this accident did not came about in advance!
I bookmarked it.
online stamp maker free online stamp maker free .
VIP Experience 유흥사이트
Meds information sheet. Cautions.
where to buy cheap trazodone price
All about medicament. Read now.
slot siteleri: en kazancl? slot oyunlar? – slot oyunlar?
Hi there, You have done an excellent job. I’ll certainly digg it and personally suggest to my
friends. I’m sure they will be benefited from this web site.
I’m truly enjoying the design and layout of your
blog. It’s a very easy on the eyes which makes it much more pleasant for me
to come here and visit more often. Did you hire out a designer to create your theme?
Great work!
5 Killer Quora Answers On Best Car Locksmith Near Bedfordshire
Car locksmith near Bedfordshire
전남쪽은 저희 전남호빠가 꽉 잡고 있습니다.
guvenilir casino siteleri: en guvenilir casino siteleri – canl? casino siteleri
slot oyunlar? puf noktalar?: slot siteleri – slot oyunlar? puf noktalar?
This was a wonderful guide. Check out hier überprüfen for more
http://www.airlines-inform.ru/personal/user/?UID=73969 [url=www.www.liveinternet.ru/users/promokodherb/profile]www.www.liveinternet.ru/users/promokodherb/profile[/url] .
Skup nieruchomości to idealna opcja dla tych, którzy potrzebują natychmiastowej gotówki za swoją nieruchomość. Dzięki temu procesowi można uniknąć długotrwałych formalności związanych z tradycyjną sprzedażą skup mieszkań Warszawa
deneme bonusu veren siteler: deneme bonusu veren siteler yeni – deneme bonusu veren siteler
I like the helpful information you supply for your articles.
I’ll bookmark your weblog and take a look at once more right here frequently.
I am relatively sure I will be informed plenty of new stuff proper
right here! Good luck for the next!
slot siteleri: en cok kazand?ran slot oyunlar? – slot casino siteleri
Common Poker Mistakes Newer Poker Players Often Unwittingly Make 에볼루션 티비
É maravilhoso ver como a comunidade brasileira se junta em Paris! Histórias inspiradoras e muitas dicas legais por aí, não posso perder o guia brasileiro na frança
Adult Entertainment 오피
What’s The Job Market For Repairs To Double Glazing Windows Professionals?
repairs to double glazing windows, Viola,