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
The 10 Most Scariest Things About Slot Bonuses video slots (bookmarkzones.Trade)
You’ll Never Be Able To Figure Out This 2 In 1 Highchair’s Tricks 2
In 1 Highchair – Emplois.Fhpmco.Fr –
From Around The Web Twenty Amazing Infographics About Case Opening Sites CSGO Case Operation Bravo
So You’ve Bought Car Accident Law … Now What? car Accident Lawyer
A Provocative Remark About Slot Machine Games evoplay slots games
Regards! Plenty of info.
Feel free to visit my web blog https://entertowin.xyz/free-cpa-marketing-courses-over-6tb
Do you mind if I quote a couple of your posts as long as I provide
credit and sources back to your website? My blog is in the exact same niche as yours and my
users would really benefit from some of the information you present here.
Please let me know if this ok with you. Thank you!
10 Things You’ve Learned In Kindergarden They’ll
Help You Understand Federal Employers lawsuit
Foldable Under Desk Treadmill Tools To Make
Your Everyday Lifethe Only Foldable Under Desk Treadmill Trick That Should Be Used By Everyone
Be Able To Foldable Under Desk Treadmill
10 Misleading Answers To Common Online Shopping Top
7 Questions: Do You Know The Right Answers? Residential American Flagpole
What’s The Current Job Market For Slots For Fun Professionals?
Slots For Fun
The Most Popular Slot Apps Is Gurus. 3 Things Best payouts
slots, https://www.hulkshare.Com/cancercotton56/,
Guide To Window Repair: The Intermediate Guide To Window
Repair Window Repair
Drug information. Short-Term Effects.
can you buy generic indomethacin without insurance
Some about pills. Get information here.
The 10 Most Terrifying Things About Upvc Door Repairs Near Me upvc Door repairs Near me
Slot Machine Games: What No One Is Talking About evoplay slots rewarding [Lilly]
Thank you for the good writeup. It in fact was a amusement account it.
Look advanced to more added agreeable from you!
By the way, how can we communicate?
Birth Defect Case Tips From The Top In The Industry birth defect lawsuits
Amazing loads of excellent material.
Medical Malpractice Attorneys Is The Next Hot Thing In Medical Malpractice
Attorneys lawyers
11 Methods To Redesign Completely Your Upvc Window Repairs window repairs near Me
12 Statistics About Slot Strategies To Inspire You To Look More Discerning Around The Cooler Water Cooler slot strategy
20 Trailblazers Lead The Way In Glass Repair Cambridge Double Glazing Near Me
You definitely made the point!
10 Life Lessons We Can Take From Real Money Slots online slots – http://jpandi.co.kr/bbs/board.php?bo_table=free&wr_id=144268
–
4 Dirty Little Secrets About Double Buggy Industry Double Buggy
Industry tandem double buggy
5 Malpractice Settlement Lessons Learned From The Pros malpractice Lawsuit
Your mode of describing all in this article is truly fastidious,
all be capable of simply know it, Thanks a lot https://www.cliniquedelenfant.ca/formations-et-conferences/
I have read so many content on the topic of the blogger lovers
however this article is really a good article, keep it up.
Why Auto Accident Lawsuit Isn’t As Easy As You Imagine Auto Accident law firm (https://freemaple.Today/)