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
dapoxetine price: dapoxetine online – buy dapoxetine online
BBgate MarketPlace 2024 Breaking Bad Gate Forum
BBgate MarketPlace
Приветствую. Подскажите, где почитатьразные блоги о недвижимости? Пока нашел – https://finkom74.ru
darknet market darknet markets 2024 https://darknetmarketstore.com/ – dark web market
Hi there, after reading this remarkable piece of writing i am as well happy to share my familiarity here
with colleagues.
I don’t even know how I ended up here, but I thought this post was good.
I do not know who you are but definitely you are going to a famous blogger if you are not already
😉 Cheers!
Five Killer Quora Answers To Programing Keys programing key
[Christoper]
I am sure this post has touched all the internet people, its really really pleasant paragraph on building up new webpage.
clomid tablet: cost of clomid without insurance – generic clomid online
Someone necessarily lend a hand to make seriously articles
I might state. This is the first time I frequented your web page and thus far?
I amazed with the research you made to make this particular put
up extraordinary. Magnificent task!
priligy max pharm: priligy – priligy maxpharm
Всё, что нужно знать о покупке аттестата о среднем образовании без рисков
deep web search drug markets onion https://darknetmarketstore.com/ – dark web links
where to get generic clomid: where can i get generic clomid without rx – can i get clomid without dr prescription
Pretty! This was an extremely wonderful article. Thanks for providing this info.
This is my first time pay a quick visit at here and i am really pleassant to read all at one place.
Thanks for any other informative web site. The place else could I get that kind of info written in such
a perfect means? I’ve a project that I am just now working on, and I have been on the look out for such info.
Hmm is anyone else encountering problems with the images
on this blog loading? I’m trying to figure out if its
a problem on my end or if it’s the blog. Any feedback would be
greatly appreciated.
where to buy generic clomid online: generic clomid – get generic clomid without dr prescription
The Fitbit Charge is a good fitness tracker.. however feels somewhat
confused with the Fitbijt family.
Here is my homepage :: sgสล็อต
Всё, что нужно знать о покупке аттестата о среднем образовании без рисков
Официальная покупка диплома вуза с сокращенной программой обучения в Москве
order clomid tablets: can i buy generic clomid without rx – get cheap clomid without dr prescription
Great blog right here! Additionally your web site quite
a bit up fast! What web host are you the usage of?
Can I get your affiliate hyperlink to your host? I wish my web site loaded up as quickly as yours lol
Hello to all, because I am genuinely keen of reading this blog’s post to be updated regularly.
It consists of pleasant information.
Fantastic items from you, man. I’ve bear in mind your
stuff prior to and you’re just extremely wonderful.
I really like what you’ve bought right here, really like what
you’re saying and the best way in which you assert it. You make it enjoyable and you still take care of to keep
it sensible. I can not wait to learn much more from you.
That is really a tremendous site.
What Is Driving License C? History Of Driving License C
prawo jazdy na automat
amoxicillin 775 mg: buy amoxicillin online without prescription – amoxicillin 500mg capsules price
where can i buy clomid without rx: rex pharm – clomid buy
Very good article. I definitely love this site.
Stick with it!