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
Online poker
Hi, I think your website might be having browser compatibility issues.
When I look at your blog in Ie, it looks fine but when opening in Internet Explorer, it has
some overlapping. I just wanted to give you a quick heads
up! Other then that, amazing blog!
%%
Here is my web site … Skoda Car Keys (https://Dandelion.Hijack7.Co.Kr/)
%%
My site :: vauxhall adam car key – https://www.spectshop.com
–
Are you tired of dealing with the hassle of setting up a Cash App account?
Look no further! Buy a verified Cash App account today and
skip the verification process. Save time and enjoy the
convenience of making fast and secure transactions. Don’t wait,
get your verified Cash App account now!
double glazing In macclesfield
You can certainly see your enthusiasm in the work you write.
The arena hopes for more passionate writers like you who
are not afraid to say how they believe. At all times go after your heart.
Excellent blog here! Also your web site loads up fast!
What web host are you using? Can I get your affiliate link to
your host? I wish my website loaded up as quickly as
yours lol
Effectively spoken genuinely! .
Porn fans get a kick away of seeing a female pleasure herself. They especially want MILF’s to do it, like they’re obtaining a peek into something personal. It’s like watching fact TV – you can see what folks are doing in their own lives.
watch porn video
Everyone loves it when folks get together and share ideas.
Great site, keep it up!
Just desire to say your article is as astonishing.
The clarity in your post is simply great and i could assume you are an expert on this subject.
Fine with your permission let me to grab your RSS feed to keep up to date with forthcoming post.
Thanks a million and please carry on the enjoyable work.
%%
my web blog – programming
What’s Going down i am new to this, I stumbled upon this I’ve discovered It absolutely helpful and
it has helped me out loads. I’m hoping to give a contribution & assist other users like
its helped me. Good job.
Are you tired of going through lengthy verification processes?
Look no further, as you can now buy a verified Cash App account hassle-free!
Enjoy the convenience of instant peer-to-peer payments and seamless transactions without any verification delays.
Don’t waste time, get your verified Cash App account today!
Hello, i believe that i saw you visited my weblog so i got
here to return the want?.I am attempting to in finding things
to enhance my site!I assume its good enough to use some
of your ideas!!
my site :: top ten poker sites
Thanks very interesting blog!
I enjoy what you guys tend to be up too. Such clever work and exposure!
Keep up the good works guys I’ve included you guys to our blogroll.
%%
Feel free to surf to my blog … suzuki alto key fob
%%
My web page :: volvo key, Marylou,
Just a smiling visitor here to share the love (:
, btw outstanding layout.
Also visit my homepage; find sex near me
Wow, awesome blog layout! How long have you been blogging for?
you make blogging look easy. The overall look of your web site is magnificent, as well as the content!
%%
My blog Organic Cbd
When someone writes an post he/she retains the image of a user in his/her brain that how a user can be aware of it.
Therefore that’s why this piece of writing is outstdanding.
Thanks!
These are really enormous ideas in regarding blogging. You have touched some good
points here. Any way keep up wrinting.
Hi there I am so grateful I found your web site, I really found
you by accident, while I was researching on Askjeeve
for something else, Anyhow I am here now and would just like to say thanks a lot for a
marvelous post and a all round enjoyable blog (I also love the
theme/design), I don’t have time to read it all at the moment but I have book-marked it
and also included your RSS feeds, so when I
have time I will be back to read much more, Please do
keep up the excellent work.
%%
my blog – smart car key replacement near me – Micki
–
Mesothelioma lawyers Asbestos claims
Right now it seems like WordPress is the preferred blogging platform available
right now. (from what I’ve read) Is that what you are
using on your blog?