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
For hottest information you have to visit world-wide-web and on world-wide-web I found this
site as a best web site for hottest updates.
It’s really a nice and helpful piece of info. I’m happy
that you simply shared this useful information with us.
Please keep us up to date like this. Thanks for sharing.
I think the admin of this web page is truly working hard
in favor of his site, as here every data is quality based information.
Howdy very nice blog!! Man .. Beautiful .. Amazing ..
I’ll bookmark your website and take the feeds additionally?
I am happy to find so many helpful information here in the put up, we need develop more strategies
on this regard, thank you for sharing. . . . . .
I’ve been browsing online more than 4 hours today, yet I never found any interesting article like yours.
It is pretty worth enough for me. Personally, if all web owners and bloggers made good content
as you did, the internet will be much more useful than ever before.
Holz als Material für Fensterrahmen sind ein beliebter Klassiker.
Leider müssen Sie bei der Wahl von Holzfenstern etwas höhere Kosten als bei Kunststoff
miterleben und berücksichtigen, dass Holzrahmen regelmäßig neu nicht zustande kommen müssen, um wetterfest zu bleiben. Sie überzeugen durch eine angenehm natürlich Optik und
allgemeine Robustheit. Aluminiums größte Stärke ist seine strukturelle
Stabilität. Diesen Vorteil kann ein Aluminiumrahmen aber unzerteilbar normalen Wohnhaus nicht ausspielen und dieser Fakt
ist eher bei industriellen Gebäuden oder Hochhäusern von. Mit einem Aluminiumrahmen lassen sich Fenster bauen, die größer sind und stärker beansprucht
werden können als mit jedem anderen Material. Fürt durchschnittliche Wohngebäude dürfte
eher entscheidend sein, dass Aluminiumrahmen deutlich mehr kosten als die Alternativen. Genutzt werden sie in diesem Bereich also nun gar aus ästhetischen Gründen, wenn das Geld eine
untergeordnete Rolle spielt. In diesem VOR-ORT-Video sind wir bei einem unserer Projekte
in Hagen (bei Dortmund) dabei. Gemeinsam mit unserem Handwerkspartner LFB werden hier monadisch 12-stöckigen Hochhaus alle Fenster erneuert.
Dabei werden 420 Fensterelemente modernisiert.
We are a group of volunteers and starting a new scheme in our
community. Your site provided us with valuable info to work
on. You have performed an impressive task and our entire neighborhood will be grateful to you.
I take pleasure in, cause I discovered exactly what I was
having a look for. You’ve ended my four day lengthy hunt!
God Bless you man. Have a nice day. Bye
Hey! I know this is kind of off-topic but I needed to ask.
Does operating a well-established website such as yours require a
large amount of work? I am completely new to running a blog but
I do write in my journal everyday. I’d like to start a blog so I will be able to share my personal experience and thoughts online.
Please let me know if you have any kind of suggestions or tips for brand
new aspiring bloggers. Thankyou!
Having read this I thought it was rather informative.
I appreciate you finding the time and effort to put this short 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!
This is a very good tip particularly to those fresh to
the blogosphere. Simple but very accurate info… Thank you for sharing this one.
A must read article!
It is actually a great and useful piece of info. I’m satisfied that you shared this useful information with us.
Please keep us up to date like this. Thanks for sharing.
I every time spent my half an hour to read this web site’s
articles daily along with a cup of coffee.
Yes! Finally someone writes about https://setiweb.ssl.berkeley.edu/beta/show_user.php?userid=8648402.
I every time used to study paragraph in news papers but now as I am a user of
internet so from now I am using net for posts, thanks to web.
After I originally commented I seem to have clicked on the
-Notify me when new comments are added- checkbox and now each
time a comment is added I get four emails with
the exact same comment. Is there a means you are able to remove me
from that service? Many thanks! http://indiagolist.com
Ein Handstaubsauger ist ein besonders kompakter Sauger, der ideal ist für den schnellen Einsatz zwischendurch.
Ist der Zuckertopf heruntergefallen oder unterm Esstisch häufen sich
nach dem Frühstück die Krümel? Dann nehmen Sie schnell den Handstaubsauger bereit und saugen diese
ruckzuck weg. Die Geräte sind kompakt und
dank integriertem Akku kabellos. Sie sind also nicht für die regelmäßige Bodenreinigung
ausgelegt – das wäre aufgrund ihrer Größe auch sehr anstrengend, da auf allen vieren vonstattengehend.
Dafür fehlt es Ihnen aber meist an einer mit einem Bodenstaubsauger vergleichbaren Saugkraft.
Eine eher exotische Kategorie der Staubsauger ist die der Nasssauger, auch Waschsauger, Wasserstaubsauger oder Nass-/Trockensauger genannt.
Sie sind für die gründliche reinigung eines Teppichs gedacht, was man förmlich regelmäßig mal durchführen sollte.
Hierfür wird der Teppich nass und unter Einsatz eines speziellen Reinigungsmittels bis sehr
spät die Fasern gereinigt. Aber auch fürt Entfernen von hartnäckigen Flecken im Teppich sind sie ordentlich.
Das löst nicht nur festsitzenden Dreck und Flecken, sondern verleiht ihm oft auch wieder eine frischere
Farbe. Für den täglichen bzw. wöchentlichen Hausputz
sind Nasssauger wird er aber in der Regel nicht angefordert.
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 feed-back would be greatly appreciated.
There is certainly a lot to find out about this topic.
I love all the points you’ve made.
This article is truly a fastidious one it assists new internet visitors, who are wishing
for blogging.
It’s nearly impossible to find knowledgeable people about this
subject, however, you sound like you know what you’re talking about!
Thanks
I was curious if you ever thought of changing the layout of your site?
Its very well written; I love what youve got to say. But maybe you could a
little more in the way of content so people could connect with it better.
Youve got an awful lot of text for only having one or two images.
Maybe you could space it out better?
Wow, awesome blog layout! How long have you been blogging for?
you make blogging look easy. The overall look of your website is wonderful, as well as the content!
I have been exploring for a little bit for any high quality articles or weblog posts on this sort of space .
Exploring in Yahoo I at last stumbled upon this website. Reading
this information So i’m happy to express that I have
an incredibly just right uncanny feeling I found out just what I needed.
I such a lot definitely will make sure to don?t omit this site and provides it a look regularly.
This blog was… how do you say it? Relevant!! Finally I’ve found something that helped
me. Thank you!
Your method of explaining all in this piece of writing is genuinely nice, all can easily know it, Thanks a lot.
Oh my goodness! Impressive article dude! Thank you, However I am having issues with your RSS.
I don?t understand why I am unable to join it. Is there anyone else having similar RSS issues?
Anyone that knows the solution can you kindly respond?
Thanx!!
Way cool! Some extremely valid points! I appreciate you penning this post and also the rest of
the website is extremely good.
Just wish to say your article is as amazing.
The clearness to your publish is just great and that i can assume you’re a professional on this subject.
Well with your permission allow me to clutch your feed
to stay up to date with impending post. Thanks a million and please carry on the rewarding work.
Hello, I check your blogs regularly. Your story-telling style is witty,
keep it up!