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
купить сертификат специалиста http://ukr-diplom.ru .
my homepage :: where can i find sex near me
купить диплом медсестры купить диплом медсестры .
First off I want to say terrific blog! I had a quick question which I’d like to ask if you don’t mind.
I was curious to find out how you center yourself and clear your head before
writing. I’ve had a tough time clearing my mind in getting my ideas out.
I truly do enjoy writing however it just seems like the
first 10 to 15 minutes are lost simply just trying to figure out how to begin. Any
recommendations or hints? Kudos!
Really no matter if someone doesn’t know afterward its up to other visitors
that they will help, so here it occurs.
What Do You Think? Heck Is Best Slot Machines? legitimate slots
Unquestionably believe that which you said. Your favorite reason seemed to be on the internet the simplest thing to be aware of.
I say to you, I definitely get annoyed while people consider worries that they
plainly don’t know about. You managed to hit the nail upon the top and also defined
out the whole thing without having side-effects , people can take a signal.
Will probably be back to get more. Thanks
The Best Tips You’ll Ever Receive On Semi Truck Legal semi truck accident lawsuits
cost of cheap omnicefs prices
“The CSGO Battle Case Awards: The Top, Worst, Or Strangest Things We’ve Ever Seen Cs20 Case
We’re a bunch of volunteers and starting a new scheme in our community.
Your site provided us with useful info to work on. You’ve done a formidable job and our
whole community will probably be thankful to you.
Hey There. I found your blog using msn. This
is a really well written article. I will be sure to bookmark it and return to
read more of your useful information. Thanks for the post.
I’ll certainly comeback.
live online gambling https://www.royalspins-game.com/casino-reviews/live-casinos/ .
Five Tools That Everyone Within The Certified Slots Industry Should
Be Making Use Of slots with Bonuses (http://www.e10100.com)
The Most Common Slot Updates Debate Could Be As Black And
White As You Might Think slots with free spins (Avery)
You’ll Never Be Able To Figure Out This Treadmills
UK’s Benefits fit
Great postings, Regards.
Guide To Dangerous Drugs Law Firm: The Intermediate Guide For
Dangerous Drugs Law Firm dangerous drugs Law firm
I need to to thank you for this excellent read!! I absolutely loved
every little bit of it. I have you saved as a favorite to check out new things you post…
What’s up, I wish for to subscribe for this web site to take
newest updates, therefore where can i do it please assist.
20 Trailblazers Leading The Way In Secure Slots slot Offers; http://www.google.com.pk,
Repairs To Upvc Windows: 11 Thing You’re Not Doing window repair (http://www.qishuashua.com.cn)
The Most Underrated Companies To In The Motor Vehicle Litigation Industry Motor Vehicle Accident Attorneys
The 10 Most Terrifying Things About Slot Promotions
Slot Promotions
14 Savvy Ways To Spend Leftover Gatotkaca Slot Budget gatot Kaca
This Is The One Slot Walk-Throughs Trick Every Person Should Be Aware Of casino slot tournaments
Hi friends, its impressive post on the topic of tutoringand fully explained, keep it up all the time.
Thanks for your marvelous posting! I actually enjoyed reading it, you may be a great author.
I will make sure to bookmark your blog and may come back
at some point. I want to encourage you to ultimately
continue your great writing, have a nice holiday weekend!
This Is The New Big Thing In Free Slots Slot machine reviews
Seven Reasons Why Motor Vehicle Settlement Is So Important
motor vehicle accidents