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
Where Will Online Part Time Work From Home Be 1 Year From What Is
Happening Now? Work From Home And Earn Money (https://Www.Google.Co.Cr/Url?Q=Https://Richard-Blum.Hubstack.Net/14-Savvy-Ways-To-Spend-Left-Over-Work-From-Home-Jobs-No-Experience-Budget)
http://cenforce.pro/# order cenforce
Purchase Cenforce Online cheapest cenforce cenforce.pro
Also visit my web page; hookups near me (http://7hi.healthycalifornia.org)
buy Viagra online: Cheapest place to buy Viagra – over the counter sildenafil
Viagra without a doctor prescription Canada viagras.online viagra without prescription
https://viagras.online/# buy viagra here
The 3 Greatest Moments In Michael Kors Bag Crossbody History michael kors bags black friday – Dyan,
cheapest cialis: cialist.pro – Cialis over the counter
10 Tell-Tale Signals You Need To Find A New Double Glazing Unit Repair Double Glazing Repairs, Apricot-Pineapple-Fc3Krn.Mystrikingly.Com,
10 Methods To Build Your Window Repair Leeds Empire Upvc Window repairs leeds
http://kamagra.win/# Kamagra 100mg
The Most Innovative Things That Are Happening With Minnesota Birth Injury Attorneys Maternal Birth Injury
10 Misconceptions That Your Boss May Have Regarding 10kg Washing Machine Price 10 kg washing machine
10 Facts About Upvc Window Repairs That Will Instantly Put You In Good Mood window repairs near me (Lyle)
I like the valuable information you supply to your articles.
I’ll bookmark your blog and take a look at once more right here regularly.
I am relatively sure I’ll be told a lot of new stuff right here!
Good luck for the following!
Feel free to visit my blog post mt5 plugin
Ask Me Anything: 10 Responses To Your Questions About Locksmith For Cars Near Me locksmith for cars near me [k12.instructure.com]
10 Window Glass Repair Near Me Tips All Experts Recommend double glazed window repairs Near me
https://viagras.online/# buy viagra here
Cheap Sildenafil 100mg Cheap Viagra 100mg viagra without prescription
Automobile Locksmiths Tools To Help You Manage
Your Daily Lifethe One Automobile Locksmiths Trick That Every Person Must Be Able To Automobile Locksmiths
20 Things Only The Most Devoted Double Glazing Condensation Repair Cost Fans Should Know window replacement near Me
Hi there everyone, it’s my first pay a quick visit at this web page,
and piece of writing is really fruitful in favor of me,
keep up posting these types of articles.
17 Reasons Not To Ignore Symptoms Of Adhd In Adults Female undiagnosed adhd in adults symptoms;
Isidro,
https://cenforce.pro/# Buy Cenforce 100mg Online
The 10 Most Scariest Things About Leeds Double Glazing leeds double glazing
cheap viagra: viagras.online – Viagra tablet online
https://cenforce.pro/# cheapest cenforce
10 Things Everyone Gets Wrong About The Word “Window Sash Repairs” Replacement
10 Healthy Habits To Use Local Window Repair window doctor Near Me