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
What Freud Can Teach Us About CSGO Cases To Invest In gloves cases (Clement)
9 . What Your Parents Teach You About Self Emptying Robot Vacuums self emptying robot vacuums, Maryjo,
How Adding A Window Glass Repair Near Me To Your Life Will Make All The Impact window Repairs
https://ciprofloxacin.tech/# ciprofloxacin mail online
5 Killer Quora Answers On Rabbit Toy Adult rabbit toy adult (http://www.dermandar.com)
Why You Must Experience Psychiatrist Assessment At Least Once In Your Lifetime
Psychiatric Assessment Online
femara vs tamoxifen buy nolvadex online clomid nolvadex
Guide To Double Glazing Repairs Near Me: The Intermediate Guide The Steps To Double Glazing Repairs Near Me double glazing repairs
near me; http://dnpaint.co.kr/,
cheapest price for tadalafil
glucophage sale
9 . What Your Parents Taught You About Symptoms Of Adhd In Adults Test symptoms of adhd in adults test
These are truly enormous ideas in about blogging. You have touched some nice points here.
Any way keep up wrinting.
17 Reasons Why You Should Be Ignoring Branded Slots Top Developer Slots
where can i buy cipro online ciprofloxacin 500mg buy online cipro pharmacy
10 Things You Learned In Kindergarden To Help You Get Started With Upvc Window Repairs window repairs near me
Hi, i think that i saaw youu visited mʏ blog thus i cаme t᧐ “return the favor”.I’m tryiing to find tһings
to enhance mү website!Ӏ suppolse іts oк to սse a few оf youг
ideas!!
Ꭺlso visit my web blog Slot Pulsa
Для игроков Покердом казино действует программа поощрения.
get cheap propecia tablets cost cheap propecia online cheap propecia online
10 Ways To Create Your ADHD Diagnosis Private
Empire Local to me
14 Misconceptions Commonly Held About Private ADHD Test adhd Assessment for adults private
5 Laws That Can Help The Treating Adhd In Adults Industry Treatment of Adhd in adults
11 Methods To Totally Defeat Your Treadmill Incline Does Treadmill Incline Burn More Calories
How To Outsmart Your Boss On Local SEO Strategy local seo marketing
What’s The Reason You’re Failing At Key Car Replacement cheap car key replacement
ciprofloxacin: purchase cipro – buy cipro
17 Reasons To Not Not Ignore Ghost 2 Immobiliser
Ghost Immobiliser Price
Excellent site you have here.. It’s difficult to find excellent writing like yours nowadays.
I truly appreciate people like you! Take care!!
Ten Work For Avon Online-Related Stumbling Blocks
You Shouldn’t Share On Twitter Find
The History Of Windows Repairs In 10 Milestones
Window repair
15 Unquestionable Reasons To Love Truck Accident Attorney Near Me truck Accidents