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://cytotec.club/# cytotec abortion pill
cheap lisinopril 40 mg: buy lisinopril without prescription – price of lisinopril 20 mg
A How-To Guide For Mercedes Key From Beginning To
End Replacement Mercedes key
http://nolvadex.life/# nolvadex online
Who Is Responsible For An Couches For Sale Budget?
12 Top Ways To Spend Your Money Cheap Sofa bargains
https://nolvadex.life/# how to get nolvadex
15 Gifts For The Treatment For Adult Adhd Lover In Your
Life adhd treatment for adults uk (Modesta)
20 Resources To Make You More Efficient At Replacement Windows Leeds Upvc door repair meanwood
http://finasteride.store/# buy propecia without dr prescription
http://lisinopril.network/# online lisinopril
It’s The Complete Cheat Sheet For CS GO Gambling Sites skins
7 Little Changes That’ll Make The Biggest Difference In Your Double Glazed Repairs Near Me Double Glazing Units Near Me (https://Severinsen-Hickey-2.Thoughtlanes.Net/)
You’ll Never Guess This Double Glazed Units Near Me’s Tricks Double Glazed Units Near Me
lexapro and tamoxifen: nolvadex price – femara vs tamoxifen
What’s The Current Job Market For Locksmith For A Car Professionals?
locksmith for a car
20 Reasons To Believe Window Repair Near Will Never Be
Forgotten Upvc Window Repair Near Me
https://nolvadex.life/# tamoxifen postmenopausal
buy propecia: cost of propecia pills – cost propecia prices
15 Gifts For The Mesothelioma Attorneys Lover In Your Life mesothelioma Compensation
I have read so many content on the topic of the blogger lovers but this paragraph is
actually a pleasant post, keep it up.
Here is my blog :: sex near ne
Medical Malpractice Litigation Explained In Fewer Than 140 Characters medical malpractice Lawsuits
Check out my page … 33poker
See What Auto Locksmiths Near Me Tricks The
Celebs Are Using auto Locksmiths near me
Nine Things That Your Parent Teach You About Window Handles Replacement
Window handles replacement
Don’t Believe In These “Trends” Concerning Locksmith For Car Near Me
lost car keys Locksmith
Remarkable things here. I am very glad to see your article.
Thanks so much and I’m having a look ahead to contact you.
Will you please drop me a mail?
You’ll Be Unable To Guess Best Robot Vacuum For Dog Hair’s Benefits robot Vacuum for dog Hair
http://lisinopril.network/# how to order lisinopril online
order generic propecia pills buy cheap propecia order generic propecia without rx