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
Hello, i believe that i noticed you visited my web site
so i came to return the desire?.I am attempting to to find issues to improve my site!I suppose its adequate to use a few of your ideas!!
Feel free to visit my homepage … Off-Grid Tiny House
%%
Feel free to surf to my web page: replacement key for saab 93 (http://dhmine.co.kr)
Excellent article. Keep writing such kind of info on your site.
Im really impressed by your blog.[X-N-E-W-L-I-N-S-P-I-N-X]Hello there, You’ve done a great job.
I’ll definitely digg it and in my view suggest to my friends.
I’m confident they will be benefited from this web site.
Also visit my web page :: local hookups near me
This is my first time pay a visit at here and i am actually
happy to read everthing at alone place.
%%
Review my site – cbd oil for pets price uk (Bennie)
%%
Also visit my web-site :: suzuki ignis key (hanamsil.kr)
Superb blog! Do you have any suggestions for aspiring writers?
I’m hoping to start my own blog soon but I’m a little lost on everything.
Would you recommend starting with a free platform
like WordPress or go for a paid option? There are so many choices out there that I’m
completely overwhelmed .. Any ideas? Thank you!
Can I just say what a comfort to uncover a person that genuinely knows what they
are discussing over the internet. You definitely realize how to bring
a problem to light and make it important.
More and more people ought to check this out and understand this side of your story.
I was surprised that you are not more popular given that you certainly have the gift.
इनमें से किसी एक वेबसाइट पर क्लिक करें.
Heya i’m for the primary time here. I came across this board and I
in finding It truly useful & it helped me out much.
I am hoping to provide one thing back and help others like you helped me.
I don’t usually comment but I gotta admit regards for the post on this special
one :D.
Here is my web-site best online poker
%%
Feel free to surf to my blog post; Program
%%
My webpage … online
%%
Feel free to surf to my page :: Replacing Volkswagen Key
%%
Feel free to surf to my website; replacement smart car key;
legendsnkr.com,
Hi it’s me, I am also visiting this website regularly, this site is actually fastidious
and the viewers are genuinely sharing good thoughts.
%%
Feel free to surf to my blog post skoda key programming (Carma)
10 Things You Learned From Kindergarden That Will Help You With Washer Tumble Dryer Combo
Stop by my site – Washing machine sale
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. In my view, if all
site owners and bloggers made good content as you did, the web will be much more useful than ever before.
Door Fitting Macclesfield; Images.Google.Com.Sv,
%%
Check out my page :: volkswagen car key (vak.kr)
Hi there Dear, are you truly visiting this website regularly, if so after that you
will definitely get fastidious know-how.
%%
Visit my website … csgo cases new, Greg,
Oh my goodness! Awesome article dude! Thank you so much, However I am encountering difficulties with your RSS.
I don’t know the reason why I can’t join it. Is there anybody else getting similar RSS problems?
Anyone who knows the solution can you kindly respond?
Thanks!!
I think the admin of this web page is genuinely working hard in support of
his web site, for the reason that here every data is quality based stuff.
Nicely put. Thanks.
Thanks a lot for sharing this with all people you really understand what you’re speaking approximately!
Bookmarked. Please also visit my site =). We can have a hyperlink exchange agreement
among us!
Oh my goodness! Impressive article dude! Thak you,
However I am going thdough troubles with yohr RSS.
I don’t knmow wwhy I amm unale too jin it. Is thuere anybody lse getting the same RSS
problems? Anyine who knows tthe solution cann yyou
kindly respond? Thanx!!
%%
my homepage; renault replacement key, vn.Easypanme.com,
Havingg rea this I thought itt wass verry enlightening.
I apprewciate you spending some time andd effort to puut thiis contennt
together. I once agakn ffind mhself spending a significant anount oof timme botth readinmg andd commenting.
Butt soo what, itt was stipl woreth it!