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
14 Smart Ways To Spend Your The Leftover Erb’s Palsy Compensation Budget Erb’s palsy law Firms
where can i buy nolvadex: tamoxifen endometrium – tamoxifen hair loss
The Most Hilarious Complaints We’ve Seen About Upvc Window
Repairs window repairs near me (http://srv29897.ht-test.ru)
Why We Do We Love Lightweight 3 Wheeled Mobility Scooter (And You Should
Also!) 3 wheel scooters for sale
10 Things People Hate About Tommy Hilfiger Bag Crossbody Tommy Hilfiger Bag For Mens
get generic propecia without dr prescription how cЙ‘n i get cheap propecia pills cost generic propecia pills
alternatives to tamoxifen: should i take tamoxifen – tamoxifen and antidepressants
20 Resources To Make You Better At Chest Freezers For
Outbuildings Chest Freezer Capacity
3 Reasons Your Car Key Replacement Service Is Broken (And How To Fix
It) Broken
Duromine contains the active ingredient phentermine.
lisinopril 10 mg online no prescription lisinopril online 1 lisinopril
lisinopril 20 mg tablet cost zestril 10mg lisinopril 420 1g
See What Cheapest Automatic Folding Mobility Scooter Tricks The
Celebs Are Making Use Of cheapest Automatic folding mobility scooter
Begin By Meeting Your Fellow Windows Leeds Enthusiasts.
Steve Jobs Of The Windows Leeds Industry Upvc window repairs leeds
cytotec online purchase cytotec cytotec online
The 10 Most Worst Car Accident Compensation Failures Of
All Time Could Have Been Avoided Car Accident Lawyer
https://cytotec.club/# purchase cytotec
https://nolvadex.life/# buy nolvadex online
A Sage Piece Of Advice On Heat Pump Tumble Dryer Reviews From
The Age Of Five Heat pump tumble dryer reviews
Everything You Need To Be Aware Of Amazon Online Grocery Shopping Uk vimeo
cost of propecia propecia generics cheap propecia without dr prescription
What’s The Most Important “Myths” About Repairing Upvc Windows
Could Actually Be True Window Repair
https://cytotec.club/# Abortion pills online
Your mode of telling the whole thing in this post is actually pleasant, all be able to
easily understand it, Thanks a lot https://www.nudermacosmetique.com/produit/lotion-tonique-astringente-astringent-lotion/
The Best Mesothelioma Claim The Gurus Are Using 3 Things Asbestos Litigation
10 Startups That Will Change The Window Glass Replacement
Industry For The Better Window Glass Replacement Near Me
15 Weird Hobbies That Will Make You Better At Efoldi Mobility Scooter
For Sale private mobility scooters for sale
A Rewind What People Talked About Volvo Keyfob 20 Years Ago volvo key fob
functions; Noelia,
Five Killer Quora Answers To Replacement Porsche Keys Replacement porsche keys
15 Startling Facts About Erb’s Palsy Lawyers That You Didn’t
Know firms