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
http://mexicoph24.life/# mexico drug stores pharmacies
7 Effective Tips To Make The Most Of Your Childrens
Bunk Beds bunk beds sales
https://canadaph24.pro/# canadian pharmacy world
The Most Negative Advice We’ve Ever Seen About Injury Lawsuit Injury Lawsuit injuries
11 Ways To Completely Sabotage Your Double Glazing Shops Near Me local double glazing repair [ip-iv.ru]
What’s Happening i’m new to this, I stumbled upon this I have found It positively helpful and it has helped me out
loads. I am hoping to contribute & aid other users like its
helped me. Great job.
The 10 Most Terrifying Things About Car Locksmiths
Near Me car locksmiths near me – Arleen,
http://canadaph24.pro/# safe reliable canadian pharmacy
What’s The Current Job Market For Double Glazing Window
Replacement Professionals Like? Window Replacement
http://mexicoph24.life/# mexican pharmaceuticals online
best online pharmacies in mexico: mexican pharmacy – mexican pharmacy
See What Replacement Sash Windows Tricks The Celebs Are Using Replacement sash Windows
How Do You Explain Upvc Window Repairs To A Five-Year-Old Upvc window repairs near me
http://mexicoph24.life/# mexico drug stores pharmacies
Guide To Foldable Electric Treadmill: The Intermediate Guide In Foldable Electric Treadmill foldable electric treadmill
The Reasons Double Glazed Window Leeds Is Tougher Than You Think
Upvc Door Repairs Roundhay
Please let me know if you’re looking for a article author for your weblog.
You have some really good articles and I feel I
would be a good asset. If you ever want to take some of the load off, I’d absolutely love
to write some articles for your blog in exchange for a link
back to mine. Please shoot me an email if interested.
Thanks!
https://indiaph24.store/# india pharmacy mail order
canadian pharmacy 365: Licensed Canadian Pharmacy – pharmacies in canada that ship to the us
A Cheat Sheet For The Ultimate For Double Memory Foam
Mattresses bed double mattress (http://www.instapaper.Com)
10 Things You Learned In Kindergarden Which Will Help You With Designer Handbags For Sale near Me
20 Myths About Mercedes Replacement Key Cost:
Busted key for mercedes (telegra.Ph)
What’s The Current Job Market For Double Glazed Window Repairs Professionals?
Window Repairs
mexican online pharmacies prescription drugs: Online Pharmacies in Mexico – pharmacies in mexico that ship to usa
http://mexicoph24.life/# best mexican online pharmacies
How To Outsmart Your Boss Advanced Starter Kits advanced starter kits (Rosario)
mexican border pharmacies shipping to usa: mexico pharmacy – mexican online pharmacies prescription drugs
my canadian pharmacy online canadian pharmacy canadian drug pharmacy
Ahaa, its good dialogue on the topic of this post here at
this website, I have read all that, so now me also commenting
here.
What’s The Job Market For Replacement Windows Prices Professionals
Like? replacement Windows Prices