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 Experts On 10kg Washing Machine Deals Want You To
Be Able To Secadora 10kg
Work From Home Jobs Online Tips That Can Change Your Life Part Time Home Based Jobs
10 Tips For Windows And Doors Leeds That Are Unexpected Upvc window handle Repair leeds
Why Veleco Uk Should Be Your Next Big Obsession Veleco Scooter Reviews
7 Things You’ve Never Learned About Double Glazing Door Repairs Near Me
Double Glazing Doors
mail order pharmacy india: Generic Medicine India to USA – Online medicine home delivery
The 12 Best Memory Foam Double Mattress Accounts To Follow On Twitter Mattresses double
9 Things Your Parents Taught You About Replacement
Double Glazing Windows replacement double glazing windows
https://indiaph24.store/# reputable indian online pharmacy
кратом где купить
canadian pharmacy prices Certified Canadian Pharmacies cross border pharmacy canada
The People Nearest To Treadmills Folding Treadmills Share Some Big
Secrets Running On A Treadmill
The Little-Known Benefits Of Misted Double Glazing Repairs
Door Repair
indian pharmacies safe indian pharmacy indian pharmacy
medicine in mexico pharmacies: mexican pharmacy – mexico pharmacy
buying prescription drugs in mexico online: cheapest mexico drugs – mexican mail order pharmacies
mexican online pharmacies prescription drugs mexico pharmacy mexico drug stores pharmacies
http://canadaph24.pro/# cheapest pharmacy canada
10 Tips For Mesothelioma Claim That Are Unexpected Mesothelioma Legal
10 Tell-Tale Signals You Should Know To Know Before You Buy Lightweight 3 Wheel Mobility Scooter veleco 3 wheeled mobility scooter
All The Details Of Cheapest 10kg Washing Machines Dos And
Don’ts cheapest 10kg washing machines [Michaela]
What’s The Job Market For Lost Spare Car Key Professionals Like?
lost spare car Key
Window Repair Near Me Tips To Relax Your Daily Life Window Repair Near Me Trick That
Every Person Should Be Able To window repair near me
20 Resources That Will Make You More Successful At Cheap Beko Washing Machines
cheapest online pharmacy india best india pharmacy indian pharmacy
mexico drug stores pharmacies mexico pharmacy mexico drug stores pharmacies
Who’s The World’s Top Expert On Licensed Slots?
slot offers
What NOT To Do In The Double Glazing Showrooms Near
Me Industry double glaxing (Telegra.Ph)
The Reasons Mesothelioma Attorney Has Become Everyone’s Obsession In 2023 Asbestos Attorney
I loved as much as you will receive carried out right here.
The sketch is attractive, your authored material stylish.
nonetheless, you command get got an shakiness over that you wish be delivering the following.
unwell unquestionably come more formerly again since exactly
the same nearly a lot often inside case you shield this hike.