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
Why Adding A Double Glazing Repair Near Me To Your Life Will Make All The Difference Double Glazed Window
How Much Can Injury Lawyer Experts Earn? injury attorney
5 Reasons To Consider Being An Online Misted Double Glazing Repairs Near Me Buyer And 5 Reasons
To Not replacements
Incontestable Evidence That You Need Work
From Home Jobs Near Me online Jobs From home
mexican pharmacy: Online Pharmacies in Mexico – mexican pharmaceuticals online
https://canadaph24.pro/# canada drugstore pharmacy rx
The No. One Question That Everyone Working In Veleco Mobility Scooter Uk Needs To Know How To Answer veleco scooter for sale
Where Can You Find The Top Tier 2 Link Building Information? tier links
Guide To Mesothelioma Attorneys: The Intermediate Guide Towards Mesothelioma Attorneys mesothelioma
buy medicines online in india http://indiaph24.store/# cheapest online pharmacy india
buy medicines online in india
I am genuinely happy to read this web site posts
which carries lots of helpful information, thanks for providing these information.
http://canadaph24.pro/# cheap canadian pharmacy online
10 Quick Tips To Lock Smith For Cars car locksmith prices uk [Willis]
4 Dirty Little Secrets About Truck Attorney Near Me Industry Truck Attorney
Near Me Industry truck accident lawyer
24 Hours For Improving Mesothelioma Lawyer Asbestos Law
10 Things That Your Family Taught You About Michael
Kors Handbags michael kors bags amazon
9 Lessons Your Parents Taught You About Adult Women Toys
Adult Women Toys
Online medicine order http://indiaph24.store/# reputable indian pharmacies
Online medicine order
pharmacy website india http://indiaph24.store/# pharmacy website india
world pharmacy india
10 Situations When You’ll Need To Be Aware Of Lightweight
Folding Travel Mobility Scooters folding lightweight mobility scooters
What’s The Reason Nobody Is Interested In Locksmiths Automotive local automotive locksmiths [Renaldo]
http://indiaph24.store/# reputable indian pharmacies
Guide To Double Glazed Window Replacement: The Intermediate
Guide To Double Glazed Window Replacement double glazed window replacement
How Much Do Best Buy Mobility Scooters Experts
Make? best folding mobility scooter
Will Mesothelioma Compensation Always Rule The World?
Asbestos Attorney
20 Important Questions To Ask About Upvc Window Repairs Before
Buying It window Repairs near Me
What You Need To Do On This Amazon Online Grocery Shopping Uk
Curt 45799
mexican pharmaceuticals online: mexican pharmacy – pharmacies in mexico that ship to usa
Hey there! This post couldn’t be written any better!
Reading this post reminds me of my good old room mate!
He always kept chatting about this. I will forward this article to him.
Fairly certain he will have a good read. Thanks for
sharing!
What’s The Job Market For Situs Gotogel Terpercaya Professionals?
Situs Gotogel terpercaya