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
I love what you guys tend to be up too. This sort of clever
work and exposure! Keep up the awesome works guys I’ve incorporated you
guys to my blogroll.
Admiring the persistence you put into your blog and detailed information you provide.
It’s awesome to come across a blog every once in a while that isn’t
the same out of date rehashed material. Fantastic read!
I’ve saved your site and I’m adding your RSS feeds to my Google account.
Hello to all, how is everything, I think every one is getting
more from this web page, and your views are good in support of new
viewers.
It’s awesome to visit this web page and reading the views of all friends
on the topic of this article, while I am also zealous of getting know-how.
Hi there colleagues, good piece of writing and pleasant urging commented at
this place, I am genuinely enjoying by these.
Remarkable! Its truly remarkable post, I have got
much clear idea regarding from this article.
I’m curious to find out what blog system you are utilizing?
I’m experiencing some minor security issues with my latest blog and I’d like to find something more risk-free.
Do you have any recommendations?
Having read this I thought it was extremely informative.
I appreciate you taking the time and energy to put this article together.
I once again find myself spending a significant amount of
time both reading and posting comments. But so what, it was still worthwhile!
certainly like your web site but you have to test the spelling on quite a
few of your posts. Several of them are rife with spelling issues and I in finding it very bothersome to inform the reality
on the other hand I’ll certainly come again again.
This is a topic that is close to my heart… Thank you! Where are
your contact details though?
That is a good tip especially to those new to the blogosphere.
Simple but very precise information… Thanks for
sharing this one. A must read article!
Great post. I will be dealing with a few of
these issues as well..
An intriguing discussion is worth comment. I think that
you ought to write more about this subject matter, it might not be a taboo subject but
typically people don’t discuss these subjects.
To the next! Best wishes!!
This site definitely has all of the info I wanted concerning this subject and didn’t know who to ask.
Mit den Stöcken bringt Attrac noch eine App heraus, die
einen genauen Überblick über die Laufgewohnheiten und die zurückgelegte Strecke gibt.
Der Versandt der Stöcke erfolgt übern Hersteller.
Optisch sehr hochwertig sind die Stöcke von Alpidex.
Diese sind aus 100 % hochmodularem Carbon und Kork für den Handgriff.
Im Paket kommen, neben den Walking Stöcken noch einige verschiedene Aufsätze für die
Bodenspitzen. Der Versandt wird durch den Hersteller direkt geregelt.
Mit einem Gewicht von 138g sind die leichten Stöcke einfach
zu händeln und auch für Anfänger geeignet. Die aus Aluminium bestehenden Wanderstöcke von Glymnis sind höhenverstellbar mit einer
Maßvariablen von 36-130cm. Zu den Nordic Walking Stöcke liefert der Hersteller noch weitere Aufsätze für die Bodenspitzen und eine Packhülle.
Die robusten Modelle sind bei den Kunden sehr beliebt und konnten daher den fünften Platz bei den Amazon Bestsellern ergattern. Die verschiedenen Tests und Vergleiche haben natürlich
unterschiedliche Maßstäbe bei den Bewertungen, jedoch
sind die maßgeblichen Werte für die Qualität der Nordic Walking Stöcke bei allen Tests und Vergleichen gleich.
Hello my loved one! I want to say that this post is awesome, great
written and come with approximately all significant infos.
I’d like to peer more posts like this .
Good post. I learn something totally new and challenging on websites I
stumbleupon on a daily basis. It will always be exciting to read content from other authors and practice a little something from other
web sites.
Hi to every body, it’s my first visit of this web site; this web site contains
remarkable and really fine information in favor of readers.
Hi there everyone, it’s my first pay a quick visit at this site, and article is in fact fruitful
for me, keep up posting these types of articles or reviews.
I was recommended this web site by my cousin.
I am not sure whether this post is written by him as nobody
else know such detailed about my trouble. You are
amazing! Thanks!
Keep on writing, great job!
I really like your blog.. very nice colors & theme. Did you
make this website yourself or did you hire someone to do it
for you? Plz answer back as I’m looking to create my own blog and would like to know where u got this from.
appreciate it
Ahaa, its fastidious discussion about this post at this
place at this blog, I have read all that, so now me also
commenting at this place.
Magnificent goods from you, man. I have remember your
stuff prior to and you are simply extremely
magnificent. I really like what you’ve bought
here, really like what you are saying and the way in which by which you say it.
You’re making it entertaining and you continue to care for to keep it smart.
I cant wait to learn far more from you. That is actually
a terrific website.
I blog often and I really thank you for your information. The article has really peaked my interest.
I will take a note of your site and keep checking for new details about once a week.
I opted in for your RSS feed as well.
This piece of writing is actually a pleasant one it helps new the web users, who are wishing for blogging.
Wow! In the end I got a blog from where I can actually take valuable data
concerning my study and knowledge.
hey there and thank you for your information – I’ve definitely picked up something new from right
here. I did however expertise several technical issues
using this website, since I experienced to reload the web site
a lot of times previous to I could get it to load properly.
I had been wondering if your web hosting is OK? Not that I’m complaining, but slow loading instances times will often affect your
placement in google and can damage your high quality score if ads and marketing with Adwords.
Anyway I’m adding this RSS to my e-mail and can look
out for a lot more of your respective intriguing content.
Make sure you update this again very soon.
Remarkable! Its truly awesome paragraph, I have got much
clear idea on the topic of from this article.
This is a topic that’s near to my heart…
Thank you! Where are your contact details though?