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
You’ll Never Be Able To Figure Out This 18 Wheeler
Accident Law Firm’s Secrets 18 wheeler accident law firm
What’s The Job Market For Personal Injury Litigation Professionals Like?
personal injury law firms
This Story Behind Mobility Scooter Usa Is One That Will Haunt You Forever!
Josh
The Most Hilarious Complaints We’ve Seen About Online Shopping Sites
In Uk For Electronics High-Quality Equipment Bag
I blog frequently and I seriously thank you for your information. This great article has truly
peaked my interest. I’m going to take a note of your website and keep checking
for new information about once per week. I opted in for your
RSS feed as well.
약국에서 비아그라 파나요?
약국 외에도 비아그라는 온라인 약국이나 기타 온라인 판매점을 통해 구입할 수도 있습니다.
10 Tips For Buying Online From Uk To Ireland That Are Unexpected Coxreels T-Grade Reel
Question: How Much Do You Know About Ai Rewriter?
use ai to rewrite text – Giuseppe,
Drugs information for patients. Short-Term Effects.
buy nortriptyline without prescription
Some information about meds. Read here.
10 Beautiful Images Of Upvc Window Handle Replacement upvc Windows repair near Me
How To Ship To Ireland From Uk Techniques To Simplify Your Daily Lifethe One How To Ship To Ireland From Uk Trick That
Every Person Should Learn how to ship to ireland from uk (Felix)
Guide To Uk Online Shopping Sites Like Amazon: The Intermediate
Guide Towards Uk Online Shopping Sites Like Amazon uk online shopping sites like amazon (Stefan)
5 Reasons To Be An Online Slot Online And 5 Reasons To Not Legitimate slots – Jisuzm.tv
–
Three Greatest Moments In Mobile Automobile Locksmith History
Automotive Key Fob Locksmith Near Me
What Experts In The Field Would Like You To Know Orange Cat6 cable
Usually, it’s when you are least expecting them and are unprepared.
What Is Truck Lawyer Near Me And How To Utilize It Truck Accident Lawyer
ух ти…
Just place an advance order for cards through your eidoo https://eidoo-app.com/, and it will to be automatically attached to your account.
16 Facebook Pages That You Must Follow For Online Shop Marketers
Core i5-8300h gaming laptop
What You Can Do To Get More Out Of Your Slot
Wins slots with free spins – Telearchaeology.org,
Drugs information. Long-Term Effects.
where to buy cheap effexor price
All trends of pills. Get information here.
איזורים שונים בגוף, לא כולל את האיברים האינטימיים. עיסוי תאילנדי ממעסה לוהטת שירותי עיסוי תאילנדי ממעסות שעובדות בספא או דירות גם יודעות לעבוד היטב עם הגוף. כאן תפגוש בחורות שיגרמו לגוף שלך לחוות דברים חדשים בעצמות חזקות במיוחד. אתה חייב לעצמך ערב מהנה של אתיופית ליווי
If votes is what decides the suitability we had a person with 6.9m votes whom we chased away.
I was suggested this web site by my cousin. I’m not sure whether this post is
written by him as no one else know such detailed about my trouble.
You’re wonderful! Thanks!
The Online Shopping Uk Cheap Case Study You’ll Never Forget
Balanced Armature Earphones
15 Incredible Stats About Slot Sound Effects Slots With bonuses –
Maps.google.cat,
Greate pieces. Keep writing such kind of information on your page.
Im really impressed by it.
Hello there, You have performed a fantastic job. I’ll definitely digg it and in my
opinion recommend to my friends. I am sure they will be benefited
from this web site.
I’m not that much of a online reader to be honest but your
sites really nice, keep it up! I’ll go ahead and bookmark your site to come back down the road.
Cheers
10 Meetups On Slot Rtp You Should Attend Best payouts slots, Bybak.com,
Hey there! This is my first visit to your blog! We
are a group of volunteers and starting a new initiative in a community in the same niche.
Your blog provided us useful information to work on. You have
done a wonderful job!