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
Thank you for the good writeup. It in fact was a amusement account it.
Look advanced to more added agreeable from you! However, how could we communicate?
My homepage :: online poker ranking
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.
kudos
%%
my site – suzuki swift key replacement (Rodrigo)
ilford door panels – Florrie –
Good day! I simply would like to offer you a huge thumbs
up for your excellent information you’ve got here on this post.
I am coming back to your blog for more soon.
Also visit my homepage; online poker ranking
lawsuit for mesothelioma
I like the valuable info you provide in your articles.
I’ll bookmark your weblog and check again here regularly.
I’m quite sure I’ll learn many new stuff right here! Best of
luck for the next!
%%
my webpage – renault trafic key fob (Dannielle)
%%
my website … nissan qashqai key replacement (Mariel)
What’s up mates, fastidious article and pleasant arguments commented here,
I am genuinely enjoying by these.
Bow Window repair
door fitter reading; te.Legra.Ph,
If some one desires to be updated with hottest technologies then he must
be pay a visit this web site and be up to date all the time.
Hey there! I know this is kinda off topic however ,
I’d figured I’d ask. Would you be interested in exchanging links or maybe guest authoring a blog post or vice-versa?
My site discusses a lot of the same topics as yours and I feel we could greatly
benefit from each other. If you are interested feel free to send me an e-mail.
I look forward to hearing from you! Terrific blog by the
way!
Hi friends, how is all, and what you wish for to say regarding this post, in my view
its really amazing in favor of me.
It is really a nice and helpful piece of info.
I’m glad that you just shared this helpful info with us.
Please stay us informed like this. Thank you for sharing.
%%
My website: Asbestos settlement
Hi there I am so grateful I found your blog, I
really found you by accident, while I was searching on Google for something else, Regardless I
am here now and would just like to say kudos
for a tremendous post and a all round enjoyable blog (I also love the theme/design), I don’t
have time to go through it all at the minute but I have
bookmarked it and also added in your RSS feeds, so when I have time
I will be back to read much more, Please do keep up the fantastic jo.
Hi there! This is my first visit to your blog! We
are a collection of volunteers and starting a new initiative in a community in the same
niche. Your blog provided us valuable information to work on. You have done a wonderful job!
What’s up everybody, here every person is sharing these know-how, so it’s nice
to read this weblog, and I used to pay a quick visit this blog every day.
%%
My page Locksmith Near Me Auto
Mesothelioma lawsuit Payouts
Great beat ! I would like to apprentice while you amend your website, how can i subscribe for a weblog site?
The account aided me a appropriate deal.
I had been tiny bit acquainted of this your broadcast provided vivid clear idea
Ten Stereotypes About Coffee Bean Machine That Aren’t Always True
Also visit my homepage :: beans machines (likbezz.ru)
Hi there! Someone in my Myspace group shared this website with us so I came to look
it over. I’m definitely enjoying the information. I’m book-marking
and will be tweeting this to my followers! Superb blog and brilliant design and style.
Awesome! Its truly remarkable post, I have got much clear idea regarding from this post.
Right now it looks like WordPress is the best blogging platform available right now.
(from what I’ve read) Is that what you are using on your blog?
asbestos attorney asbestos [Selma]
%%
my blog post … boating accidents lawyer near me (Nicolas)
%%
Feel free to surf to my homepage; csgoroll case battle
(https://schulz-lorenzen.blogbright.Net/25-surprising-facts-about-case-battle-csgo)