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
tamoxifen premenopausal: does tamoxifen make you tired – does tamoxifen make you tired
The 10 Most Terrifying Things About ADHD Assessment UK
Private adhd Assessment uk private
17 Signs To Know If You Work With List Of Online Shopping
Sites Uk Safavieh Rug Collection [Kellye]
What’s The Job Market For Replacement Window Glass
Near Me Professionals Like? replacement window Glass
15 Weird Hobbies That Will Make You Smarter
At Car Locksmith locksmith car keys near me – Gilda,
20 Insightful Quotes About Skin So Soft Bath Oil Uk Avon Skin So Soft Spray
Hi, i feel that i noticed you visited my site so i came to go back the desire?.I’m trying to find
issues to enhance my website!I guess its adequate to make use of a few of your
ideas!!
Abortion pills online: cytotec abortion pill – cytotec pills buy online
20 Trailblazers Lead The Way In Fridge Freezers Side By Side small size side by side refrigerator (Samara)
We stumbled over here different website and thought I may as well check
things out. I like what I see so now i am following you.
Look forward to checking out your web page yet again.
propecia for sale: propecia without insurance – get propecia online
The 10 Most Terrifying Things About Adhd In Older Females Symptoms adhd in older females symptoms
See What Ford Fiesta Replacement Key Cost Uk Tricks
The Celebs Are Making Use Of Ford fiesta Replacement key
cost for generic lisinopril lisinopril diuretic lisinopril 25 mg
10 Steps To Begin The Business You Want To Start Mesothelioma
Law Business Mesothelioma case
Ten Common Misconceptions About Online Shopping Uk Amazon That Don’t
Always Hold which supermarket is cheapest for online shopping [Arden]
lisinopril 2 lisinopril online canadian pharmacy zestoretic generic
lisinopril otc: buy lisinopril 2.5 mg – lisinopril 100mcg
5 Laws Everybody In Adhd Adult Treatment Should Be Aware Of Private
http://ciprofloxacin.tech/# cipro
15 Gifts For The Leeds Window Repair Lover In Your Life new glazing leeds – Jonelle –
I blog frequently and I truly thank you for your information. This great article has truly
peaked my interest. I am going to take a note of your site and keep
checking for new information about once a week.
I opted in for your RSS feed too.
A Brief History Of Nespresso Machines In 10 Milestones
Nespresso Machines White
Instagram users share their photos, and videos with their followers or the public.
15 Gifts For The Which Is Best For Online Grocery Shopping
Lover In Your Life Home Firefighting Hf-S14Fc-150F-Bk (Hassie)
How Window Repair Near Me Has Transformed My Life The Better companies
Every weekend i used to visit this website, because i
wish for enjoyment, since this this website conations in fact nice funny material too.
Nine Things That Your Parent Teach You About Accident Case
accident
What Experts In The Field Would Like You To Be Able To
New Glazing Leeds (https://709H.Com)
Pretty great post. I just stumbled upon your blog and wanted to mention that I’ve truly
loved browsing your blog posts. After all I’ll be subscribing on your feed and I’m hoping you write again very soon!