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
I’d like to find out more? I’d like to find out more details.
The Three Greatest Moments In Designer Handbags For Work History Brand
http://kamagra.win/# Kamagra 100mg price
5 Killer Quora Answers On Mobility Scooters Near Me For Sale Mobility scooters
5 Best Online Shopping Uk Clothes Projects For Any Budget Vimeo.com
10 Things We All Are Hateful About Accident Attorney Vimeo
The 10 Most Terrifying Things About Double Glazing Repairs Near
Me double glazing repairs near me (Verona)
7 Easy Tips For Totally Rocking Your Leeds Door Panels window hinges leeds (http://www.df100.Cn)
Levitra 10 mg best price: levitrav.store – Generic Levitra 20mg
How To Create An Awesome Instagram Video About Porsche Spare Key Porsche keyfob
sildenafil 50 mg price: viagras.online – cheap viagra
Unexpected Business Strategies That Helped Lg Side By
Side Fridge Freezer Succeed Side By Side Freezers Fridge
The Biggest Problem With Double Glazed Window Repair,
And How You Can Fix It Upvc Door Repairs Near Me
https://cenforce.pro/# cenforce for sale
15 Best Documentaries On Windows And Doors Leeds upvc
door repair leeds (Harris)
Its like you read my mind! You appear to know a lot about this, like you wrote the book in it or something. I think that you could do with some pics to drive the message home a little bit, but other than that, this is excellent blog. A fantastic read. I’ll certainly be back.
16 Must-Follow Facebook Pages For Volkswagen Keys Marketers Where To get a volkswagen key cut
Cenforce 150 mg online: buy cenforce – Buy Cenforce 100mg Online
The Reasons To Focus On Enhancing Private ADHD Assessment UK adult adhd assessment Private
What Is Designer Handbags On Sale And How To Use It Designer Handbags And Purses
15 Reasons Not To Ignore Auto Lock Smith Auto Lock Smiths
Tod will be the name I love be called with and my wife doesn’t
as it at nearly all. Doing interior design is regarded as the things
i love most. My house is now in District of Columbia but I am going to have in order to maneuver in annually or a couple of.
The job he’s been occupying for years is your working personal computer operator.
The Reason Why Everyone Is Talking About Glazing Repairs Near Me Right Now Double Glazing Repair
10 Quick Tips For Avon Rep Starter Kit avon rep Starter Kit
5 Killer Quora Answers On Sash Window Repair window repair [Maismile.co.Kr]
How To Get More Value From Your Heatpump Dryer heat Pump
dryers – Agriexpert.kz,
Kamagra Oral Jelly: kamagra pills – Kamagra tablets
It’s The Evolution Of Door Fitting Leeds window Hinge repair Leeds
Wow, wonderful weblog format! How long have you ever been blogging for?
you made running a blog look easy. The overall look of your site is fantastic,
let alone the content!
Buy Viagra online cheap: Cheap Viagra 100mg – Generic Viagra online