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
Howdy! This blog post couldn’t be written much better!
Going through this post reminds me of my previous roommate!
He continually kept preaching about this. I will forward this article
to him. Pretty sure he’ll have a very good read.
Thanks for sharing!
Greetings! Very useful advice in this particular article!
It’s the little changes that will make the
most important changes. Thanks for sharing!
ordering drugs from canada canadian discount pharmacy reputable canadian pharmacy
What’s The Reason Everyone Is Talking About Uk Online Shopping Sites For Mobile Right Now Adjustable Swivel Chair
Присоединиться может любой игрок в удобное для него время.
15 Things You’re Not Sure Of About Window Seal Replacement window replacement companies
10 Factors To Know Concerning Mesothelioma
Lawsuits You Didn’t Learn At School mesothelioma Claim
mexican rx online: cheapest mexico drugs – medicine in mexico pharmacies
Ԍreat article! Thhat iіs the type of info that are meant to be shared
aϲrоss the internet. Shame on the searcһ engines
foг now not ρositioning this submit upper! Come on over and ttalk over wwith my
web ѕite . Thank you =)
Here is my website :: ck4d login
canada drug pharmacy: Certified Canadian Pharmacies – legit canadian pharmacy online
canada pharmacy online legit: canadian pharmacy prices – canadian drug pharmacy
mexico pharmacy: cheapest mexico drugs – purple pharmacy mexico price list
https://indiaph24.store/# pharmacy website india
banking methods top online casino.
https://indiaph24.store/# Online medicine home delivery
5 Things Everyone Gets Wrong About Emergency Car Locksmith
car locksmith near me
The Best Automatic Folding Mobility Scooters Tricks For Changing Your Life best Automatic folding mobility scooter
5 People You Should Be Getting To Know In The Designer Handbags And Purses Industry Online Designer Handbags
A Look Inside Double Glazing Repair Luton’s Secrets Of Double Glazing Repair Luton window Repair near me
https://mexicoph24.life/# mexico drug stores pharmacies
How Nissan Qashqai Key Replacement Has Changed My
Life The Better Nissan juke Spare key
10 Times You’ll Have To Learn About Avon Book avon new book (0522445518.ussoft.kr)
mexican rx online Online Pharmacies in Mexico medication from mexico pharmacy
A Proficient Rant About Black Fridge Freezer
best 70 30 fridge freezer – yogaasanas.science,
https://indiaph24.store/# best online pharmacy india
How To Find Out If You’re Are Ready For
Become A Avon Representative become a avon representative – Uta,
Could Misted Double Glazing Repairs Near Me Be
The Key To Dealing With 2023? double glazing door [test.gitaransk.ru]
I was recommended this blog by my cousin. I’m not
sure whether this post is written by him as no one else know
such detailed about my trouble. You’re amazing!
Thanks!
Don’t Buy Into These “Trends” Concerning Car Door Key Lock
Repair car central locking system repair near me
top 10 online pharmacy in india http://indiaph24.store/# buy prescription drugs from india
india pharmacy mail order