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
Auto Lock Smith Near Me: The Ugly The Truth About Auto
Lock Smith Near Me Auto keys locksmith
14 Cartoons About Double Glazed Doors Near Me That’ll Brighten Your Day double glazed window units [Margaret]
A How-To Guide For Windows And Doors Leeds From Beginning To End upvc Hinge repair Leeds
Hello, every time i used to check website posts here early in the dawn, as i enjoy to find
out more and more.
Aw, this was a very good post. Taking a few minutes and actual effort to
create a superb article… but what can I say… I
procrastinate a lot and never manage to
get anything done.
7 Helpful Tips To Make The The Most Of Your Double Glazed Window Leeds upvc window hinge repair leeds
ciprofloxacin: cipro ciprofloxacin – cipro ciprofloxacin
Abortion pills online buy cytotec pills buy cytotec in usa
ciprofloxacin over the counter buy cipro cheap cipro ciprofloxacin
https://nolvadex.life/# tamoxifen mechanism of action
cost generic propecia for sale: generic propecia for sale – order propecia
5 Things Everyone Gets Wrong On The Subject Of Private Psychiatrist London private Psychiatrists uk
10 Apps That Can Help You Control Your Best Case CSGO Open Cs2
http://cytotec.club/# buy cytotec
20 Things You Need To Know About Replacement Bentley Key bentley Valet key
This Is A Repair Upvc Window Success Story You’ll
Never Be Able To double glazing repairs (Edna)
What CSGO Cases Should I Open: The Good, The
Bad, And The Ugly falchion case (Sheree)
cipro online no prescription in the usa: where can i buy cipro online – purchase cipro
20 Reasons To Believe Fridge Freezers For Sale Will Never Be Forgotten Retro Fridge Freezer Sale
16 Must-Follow Pages On Facebook For Upvc Window Repairs-Related Businesses upvc window repairs near me
Cytotec 200mcg price: buy cytotec pills – buy cytotec over the counter
How To Make An Amazing Instagram Video About Upvc Windows Repair
Upvc Window Repair
Why Adhd Treatments Is Fast Increasing To Be The Most Popular Trend In 2023 nhs adhd Treatment
Private Care For Mental Health Strategies That Will Change
Your Life mental health online assessment
zestril lisinopril 25 mg price lisinopril 40 mg price
Are Avon Book Uk Just As Important As Everyone Says?
avon book April (Kelli)
20 Resources That’ll Make You More Efficient With Uk Online Shoe Shopping Websites vimeo
raloxifene vs tamoxifen: how to prevent hair loss while on tamoxifen – who should take tamoxifen
order cytotec online: buy cytotec pills – buy cytotec online
https://cytotec.club/# buy cytotec online