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
Nice blog here! Also your web site loads up very fast!
What host are you using? Can I get your affiliate link to your host?
I wish my site loaded up as quickly as yours lol
When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a
comment is added I get several e-mails with the saqme comment.
Is there anny way you can remove me from that service? Many
thanks!
Also visit my website https://www.scoop.it/topic/best-alternate-link-website
Hello there, You have done a great job. I
will certainly digg it and personally suggest to my friends.
I am sure they’ll be benefited from this website.
Regards for helping out, excellent information.
Rupanya Bonus Berlimpah
Adapun keuntungan yang bakal dapat kalian temukan dengan bermain pada permainan judi slot online berikutnya adalah anda bisa mendapatkan peluang bonus menarik.
Ini terang berbeda kalau anda bermain pada situs agen darat, yang
mana bonus akan sungguh-sungguh terbatasi. Dengan banyaknya bonus berlimpah
ini, maka tentunya akan berimbas kepada keuangan anda
nantinya.
I couldn’t resist commenting. Perfectly written!
Hi there, I found your website by means of Google while
searching for a comparable matter, your web site got here up, it seems good.
I’ve bookmarked it in my google bookmarks.
Hello there, simply become alert to your blog via Google, and located that it is really informative.
I’m gonna watch out for brussels. I’ll be grateful in case you proceed this in future.
Many folks will be benefited from your writing. Cheers!
My brother recommended I might like this blog.
He was once totally right. This put up truly made my day.
You cann’t believe simply how so much time I had spent
for this information! Thanks!
Fastidious response in return of this query with firm arguments and describing the whole thing concerning
that.
Was ist der Unterschied zwischen Kassenrollen,
Bonrollen und Thermorollen? Grundsätzlich handelt es sich bei
den Begriffen Kassenrollen und Bonrollen um Oberbegriffe und Synonyme
für Rollen die in Kassen oder Bondruckern verwendet werden. Bei Thermorollen handelt es sich um Spezialrollen, die nur nebst Thermodruckern Verwendung finden können. Für Kassen und Drucker mit Nadel oder Walzdruckwerk wäre
die entsprechenden “Spezialrolle” eine Normalpapierrolle. Sie sind auf einer Seite mit einer Schicht überzogen, die unter Wärmeeinwirkung schwarz wird
und so das Druckbild ergibt. Fälschlicherweise hat
sich die Bezeichnung Bonrollen jedoch als Synonym für eben diese Normalpapierrollen durchgesetzt.
Warum an der Zeit sein beim Kauf von Kassenrollen auf die Qualität
achten? Mit besonders billigen Kassenrollen können verschiedene Probleme auftauchen. Im bereich der Thermorollen kann sich eine
mangelhafte Papierqualität am schnellen Ausbleichen des Drucks
nicht schweigend dasitzen. Kaum feststellbar hingegen sind die im Papier eingesetzten Chemikalien. Bei Kassenrollen aus Normalpapier und Thermopapier gleichermassen kann es bei schlechter Papierqualität zu verstärkter
Staubentwicklung kommen, der sich in Ihrem Drucker oder Ihrer Kasse absetzt par exemple die Lebensdauer erheblich verkürzen kann.
I am really loving the theme/design of your
weblog. Do you ever run into any internet
browser compatibility problems? A small number of my blog audience have complained about my site not operating
correctly in Explorer but looks great in Opera. Do you have any advice to help fix this
issue?
Hey there! I just want to give you a huge thumbs up for your
great info you have got right here on this post. I will be returning to your website for more soon.
It’s difficult to find well-informed people in this particular
topic, however, you sound like you know what you’re talking about!
Thanks
Wonderful, what a webpage it is! This weblog provides helpful information to us, keep it up.
Really when someone doesn’t understand afterward its up to other viewers that they will assist, so here it takes place.
At this moment I am going to do my breakfast, later than having my breakfast
coming again to read other news. http://thaijobtips.com
I genuinely appreciate your work, Great post.
What’s up it’s me, I am also visiting this website
regularly, this web page is really fastidious and the people are in fact sharing fastidious thoughts.
Great article, totally what I needed.
Howdy! I could have sworn I’ve been to your blog before but after going through many
of the articles I realized it’s new to me. Regardless,
I’m certainly delighted I discovered it and I’ll
be bookmarking it and checking back often!
Hey there! I’ve been reading your blog for some time now and finally got the courage to go ahead and give
you a shout out from New Caney Tx! Just wanted to say keep up the
excellent work!
Hey would you mind letting me know which webhost you’re using?
I’ve loaded your blog in 3 different web browsers and I must say this blog loads a lot
faster then most. Can you suggest a good internet hosting provider at a honest price?
Thank you, I appreciate it!
Hey! This post could not be written any better! Reading
this post reminds me of my old room mate! He always kept chatting about this.
I will forward this page to him. Pretty sure
he will have a good read. Thanks for sharing!
Awesome site you have here but I was wanting to know if you
knew of any community forums that cover the same topics discussed in this article?
I’d really love to be a part of community where I can get opinions from other knowledgeable individuals that share the
same interest. If you have any recommendations, please let me know.
Thanks!
I like reading a post that will make men and women think.
Also, thank you for allowing for me to comment!
I really like your blog.. very nice colors & theme.
Did you create 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 find out where u
got this from. appreciate it
Excellent article. I am experiencing a few of these issues as well..
I believe that is among the most significant information for me.
And i am satisfied reading your article. But should remark on few basic things, The
website style is ideal, the articles is in point of fact great
: D. Just right job, cheers
Thankfulness to my father who stated to me on the topic of this web site, this website is really awesome.
Spot on with this write-up, I absolutely believe this website needs a great deal more attention. I’ll
probably be returning to read through more, thanks
for the info!