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
Hey there outstanding website! Does running a
blog like this take a large amount of work? I’ve virtually no knowledge of
coding however I had been hoping to start my own blog in the near future.
Anyway, if you have any recommendations or techniques for new blog owners please share.
I know this is off subject but I simply needed to ask.
Many thanks!
This website was… how do I say it? Relevant!! Finally I’ve found something which helped me.
Thank you!
Deneme Bonusu Veren Siteler en iyi deneme bonusu veren siteler Casino Siteleri
Valuable information! Discover more at cremation services
sweet bonanza slot sweet bonanza demo oyna sweet bonanza
casino bahis siteleri: Casino Siteleri – en guvenilir casino siteleri
Howdy just wanted to give you a quick heads up and
let you know a few of the pictures aren’t loading
correctly. I’m not sure why but I think its a linking issue.
I’ve tried it in two different internet browsers and both show
the same results.
Thanks for the thorough analysis. Find more at roofers near me
Have you ever considered about adding a little bit more than just your articles?
I mean, what you say is important and everything. Nevertheless
just imagine if you added some great images or videos to give your posts
more, “pop”! Your content is excellent but with pics and
video clips, this website could undeniably be one of the best in its niche.
Very good blog!
https://expert-kaluga.ru/
Spot on with this write-up, I absolutely feel this amazing site needs a great deal more attention. I’ll probably be back again to see more, thanks
for the advice!
My web page Small business web design
Thanks for the great explanation. More info at fire damage restoration services
http://slotsiteleri25.com/# slot siteleri
slot casino siteleri: az parayla cok kazandiran slot oyunlar? – slot oyunlar?
Hi there just wanted to give you a quick heads up.
The text in your article seem to be running off the screen in Firefox.
I’m not sure if this is a format issue or something to
do with browser compatibility but I thought I’d post to let you know.
The design look great though! Hope you get the issue solved soon. Many thanks
My web site: donat89 login
https://slotsiteleri25.com/# en cok kazand?ran slot oyunlar?
Hello there! This post couldn’t be written any better!
Reading through this post reminds me of my good old room mate!
He always kept chatting about this. I will forward this write-up to him.
Fairly certain he will have a good read. Many thanks for sharing!
Brushing my canine made use of to be a task mobile dog grooming prices
где можно купить диплом повара
Very great post. I simply stumbled upon your blog and wanted to say that I have truly loved browsing your weblog posts. In any case I?ll be subscribing to your rss feed and I hope you write again soon!
Heya! I’m at work browsing your blog from my new iphone 4!
Just wanted to say I love reading your blog and
look forward to all your posts! Keep up the outstanding work!
Maid services can help reduce stress in your life—who wouldn’t want that? Explore your options at house cleaning
Medicament information sheet. Drug Class.
can i get generic paxil
Actual information about drug. Read information here.
Fun Different Date Ideas 대전유흥
Karaoke Bar hiop
Incontestable Evidence That You Need Car
Accident Lawyers accident car lawyer (Suzanne)
Howdy superb blog! Does running a blog similar
to this require a large amount of work? I’ve virtually no understanding
of coding but I was hoping to start my own blog in the near future.
Anyhow, if you have any ideas or techniques for new blog owners please share.
I know this is off topic but I simply had to ask. Thank you!
https://rlkk.ru — Все для вашего удобства: каталог, консультации и профессиональный сервис.
Thanks for the great tips. Discover more at residential roofers Shelbyville IN
I’ve been an energetic user of LiveJasmin for quite
some time currently, and I’ve checked out various ways
to maximize my experience on the system without breaking the
financial institution. If you are looking for ways to
obtain free LiveJasmin credit reports quickly, I’ve
obtained a couple of tips that might help. These suggestions are based
upon my own experiences and what I’ve learned along the road.