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
https://cialist.pro/# Tadalafil price
How To Outsmart Your Boss On Mobility Scooters Sale second Hand shoprider mobility scooters for Sale
http://kamagra.win/# buy kamagra online usa
Why You Must Experience Defra Exempt Wood Burner At Least Once In Your Lifetime defra certified stoves (extension.unimagdalena.edu.co)
Great site. Lots of helpful info here. I’m sending it to several pals ans also sharing
in delicious. And naturally, thank you in your effort!
Buy Viagra online cheap Cheapest place to buy Viagra sildenafil 50 mg price
https://cenforce.pro/# order cenforce
This Story Behind Audi Lost Key Can Haunt You Forever!
Services
20 Resources That Will Make You More Successful At beko Washing machine deals – https://Mozillabd.science – Washers
Hey! Do you know if they make any plugins to safeguard against hackers?
I’m kinda paranoid about losing everything
I’ve worked hard on. Any tips?
Where Can You Get The Most Reliable Order
Avon Online Information? order avon online, Beulah,
10 Quick Tips On Car Key Programmer car key cut and program
Cheap Levitra online buy Levitra over the counter buy Levitra over the counter
Ten Online Clothes Shopping Websites Uk That Will Make Your Life Better Retro Dining Chair
The Ultimate Guide To 18 Wheeler Wreck Lawyer Vimeo
You’ll Never Be Able To Figure Out This Shopping Online Uk To
Ireland’s Tricks Shopping Online Uk To Ireland
http://levitrav.store/# Levitra tablet price
buy Kamagra kamagra.win Kamagra Oral Jelly
Why No One Cares About Bags Prada Prada bags For sale
10 Mistaken Answers To Common Double Mattress Size Questions Do You Know The Right Ones?
Double Mattress For Sale (https://Kristensen-Bugge.Thoughtlanes.Net/11-Strategies-To-Refresh-Your-Mattresses-Double-Size-1711503819)
10 Unexpected Top Table Freezer Tips compact Freezers
kamagra: buy kamagra online – Kamagra Oral Jelly
http://viagras.online/# Generic Viagra for sale
How To Tell If You’re Ready To GSA SER Help Gsa ser software
Cialis 20mg price in USA Generic Cialis without a doctor prescription cialis for sale
Levitra generic best price: Buy Vardenafil 20mg – Buy Vardenafil online
I’ve been exploring for a little bit for any high-quality articles or blog posts on this kind of area .
Exploring in Yahoo I at last stumbled upon this site.
Reading this info So i am glad to show that I’ve a very good uncanny feeling I discovered exactly what I needed.
I so much surely will make sure to do not omit this web site and provides
it a look on a constant basis.
My blog post – solar hot water
Five Killer Quora Answers To Motorcycle Accident
Legal Ada motorcycle accident lawyer
10 Factors To Know On Birth Defect Litigation You Didn’t Learn At School
birth Defect lawsuit
9 Lessons Your Parents Taught You About France Online Shopping Sites Clothes france online shopping sites clothes