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
Upvc Windows Bishops Stortford
It’s amazing to pay a visit this site and reading the views
of all mates regarding this piece of writing, while I am also eager of getting experience.
my blog … sex near me
Paragraph writing is also a excitement, if you be familiar with after that
you can write otherwise it is complicated to write.
hi!,I love your writing so a lot! share we keep in touch extra approximately your post on AOL?
I need a specialist on this area to solve my problem.
Maybe that is you! Having a look ahead to see you.
Ԝe are a grߋup of volunteers aɑnd starting a brand new scheme in our ϲommunity.
Your site proᴠided uss ѡith useful nformation to work on. You’ve done a formidale
taѕk and our entire neіghbоrhood ԝikl
be thankful to you.
Also visit my blog :: Các loại đồng hồ nữ
commercial windows bishops stortford
I’m very happy to uncover this page. I need to to thank you for your time just
for this wonderful read!! I definitely appreciated every part of it and I have you book marked to look at
new things on your web site.
%%
Here is my site: csgo case battle sites, https://click4r.Com/,
My relatives every time say that I am killing my time here at net, but
I know I am getting knowledge every day by reading thes nice articles.
%%
Also visit my web page; uk cbd oil (http://www.knitandpurl.co.kr)
Αmazing thingѕ here. I’m very gpad tto seee your post.
Thank you soo much aand I aam taking a look ahead to touch you.
Will you ҝidly drop mme a e-mail?
Thank you for the good writeup. It in fact was a amusement account it.
Look advanced to far added agreeable from you!
However, how can we communicate?
Take a look at my blog poker site rankings – 40th.jiuzhai.com,
%%
Feel free to surf to my website near
The People Closest To Washing Washer Dryer combinations Combo
Share Some Big Secrets
%%
Also visit my site :: replacement skoda key (Angie)
glimepiride 1mg otc
%%
Feel free to surf to my web blog; csgo Case websites – telegra.ph,
windows and doors kingston, Billy,
%%
Visit my site – Jon Zherka Twitch
Всем рекомендую!поступление в международный университет
Wow that was strange. I just wrote an really long comment
but after I clicked submit my comment didn’t show up.
Grrrr… well I’m not writing all that over again. Anyhow,
just wanted to say excellent blog!
Hello there, I think your site may be having web
browser compatibility problems. Whenever I take
a look at your web site in Safari, it looks fine however, when opening
in I.E., it’s got some overlapping issues. I merely wanted to give you a quick heads
up! Besides that, fantastic site!
In order to increase university ranking in webometrics,
proper SEO strategy is needed. Webometrics Jakarta’s
SEO services can help improve the ranking of university sites through
improving the quality of content and site visitors.
This can be done through optimizing efforts from the inside of the content and
outside of the content. Contact us at: https://bit.ly/jasawebometrics2023
That is very fascinating, You’re an overly skilled blogger.
I have joined your rss feed and stay up for searching for
more of your excellent post. Also, I have shared your web site in my
social networks
Here is my web blog; facebook local sex – Charis,
Hello there! Would you mind if I share your blog with my myspace group?
There’s a lot of folks that I think would really enjoy your content.
Please let me know. Thanks
%%
My web page; fob
Are you tired of the hassle that comes with creating a Cash
App account? Look no further! Buy a verified Cash App account and experience smooth transactions and hassle-free money management.
With a verified account, you can enjoy added security and unlock additional
features. Don’t miss out on this opportunity, get your
verified Cash App account today!
I am regular visitor, how are you everybody? This article posted at this website is in fact
nice.
free standing electric fireplaces (m.neol.piece-worker.Com)
Are you tired of dealing with the hassles of getting a verified Cash
App account? Look no further! We offer the opportunity to buy a verified account hassle-free.
With a verified account, you can enjoy the full benefits
of Cash App without any limitations. Don’t waste time, get your verified Cash App account now!