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
Five Double Glazing Door Repairs Near Me Projects For Any Budget double
glazing unit replacement (Irma)
Speak “Yes” To These 5 Mesothelioma Attorney Tips Asbestos Legal
A Retrospective A Conversation With People About
Nissan Juke Key Replacement 20 Years Ago new nissan key
Why You Should Concentrate On Improving
Psychiatrist Near Me Private psychiatrists private
https://canadaph24.pro/# thecanadianpharmacy
20 Resources To Help You Become More Efficient With Leeds
Door Panels upvc window handle repairs leeds
If you are going for best contents like I do, only pay a quick visit this web site all the time because it offers feature contents, thanks
20 Myths About Windows Leeds: Dispelled Door repair leeds
mexican online pharmacies prescription drugs: cheapest mexico drugs – reputable mexican pharmacies online
15 Of The Best Pinterest Boards All Time About CSGO Weapon Case esports 2014
summer case (Brenton)
top online pharmacy india: Generic Medicine India to USA – top 10 online pharmacy in india
Mobility Scooter For Sale Near Me: It’s Not As
Difficult As You Think mobility Scooter to buy near me
How To Recognize The Wood Burning Stove To Be Right For You Small wood burning Stoves
10 Things That Your Competitors Lean You On Motorcycle Accident Attorney motorcycle accident law firms
reputable mexican pharmacies online: Mexican Pharmacy Online – medicine in mexico pharmacies
The No. One Question That Everyone In Vauxhall Astra Key Replacement Must Know
How To Answer vauxhall spare key
You ought to be a part of a contest for one of the greatest blogs on the web.
I most certainly will highly recommend this web
site!
http://mexicoph24.life/# best mexican online pharmacies
buying prescription drugs in mexico: Online Pharmacies in Mexico – mexican online pharmacies prescription drugs
mexican border pharmacies shipping to usa: medicine in mexico pharmacies – mexican border pharmacies shipping to usa
http://mexicoph24.life/# purple pharmacy mexico price list
india online pharmacy indian pharmacy fast delivery reputable indian online pharmacy
The 10 Scariest Things About Stove Wood Burning Stove Wood Burning
с любой доставки получите несгораемые бонусные очки, https://cvetok35.ru/ которыми возможно оплачивать любые приобретения у нас.
15 Gifts For The Skin So Soft Bath Oil Uk Lover In Your Life Skin So Soft Bath Oil Uk
Feel free to visit my web site: find local hookups (Jessika)
Where Is Best Online Shopping Groceries Uk Be One
Year From Today? Easy-Install Bike Mudguard
15 Gifts For The High Roller Slots Lover In Your Life Slot Providers (Mspeech.Kr)
https://indiaph24.store/# Online medicine order
Good day! This is kind of off topic but I need some guidance from an established
blog. Is it difficult to set up your own blog? I’m not very techincal but I can figure things out pretty
fast. I’m thinking about making my own but I’m not sure where to start.
Do you have any points or suggestions? With thanks