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
This design is steller! You obviously know how to keep a reader entertained.
Between your wit and your videos, I was almost moved to start my
own blog (well, almost…HaHa!) Wonderful job. I really enjoyed
what you had to say, and more than that, how you
presented it. Too cool!
15 Unquestionable Reasons To Love CSGO Cases New gloves Cases
Buy Tadalafil 10mg Generic Tadalafil 20mg price Buy Tadalafil 10mg
cheapest cenforce: cheapest cenforce – Purchase Cenforce Online
16 Must-Follow Instagram Pages For Private Mental Health Treatment-Related Businesses how do i get a mental health assessment
sildenafil 50 mg price Buy Viagra online cheap buy viagra here
Levitra online USA fast Levitra generic best price Levitra tablet price
https://cenforce.pro/# Buy Cenforce 100mg Online
Generic Tadalafil 20mg price Generic Tadalafil 20mg price Cialis over the counter
How To Solve Issues With Double Glazed Windows Leeds wooden
11 Strategies To Completely Defy Your Cheap Online Shopping Sites
Uk Garden Sharpener Card Pack
5 Motives Windows And Doors Leeds Is Actually A Great Thing glazier
Generic Cialis price: Generic Tadalafil 20mg price – cialis for sale
Where Is Advanced Starter Kits Be One Year From This Year?
free avon starter kit (Rosalind)
Greate article. Keep writing such kind of information on your page.
Im really impressed by it.
Hi there, You have performed an excellent job.
I will certainly digg it and individually recommend
to my friends. I’m confident they will be benefited from
this site.
20 Windows Repairs Websites Taking The Internet By Storm
Window Repairs
How To Recognize The Right Designer Handbags Sale For
You Designer Handbags sale outlet
п»їBuy generic 100mg Viagra online Buy Viagra online cheap best price for viagra 100mg
Hey there! Do you know if they make any plugins to assist with SEO?
I’m trying to get my blog to rank for some targeted keywords
but I’m not seeing very good results. If you know of any please share.
Thank you!
sildenafil oral jelly 100mg kamagra: kamagra oral jelly – super kamagra
10 Inspiring Images About Double Glazing Repairs Near Me double glazing near me
https://kamagra.win/# buy kamagra online usa
Cheap generic Viagra: viagra without prescription – Buy Viagra online cheap
The Next Big Thing In Nissan Key Fob Replacement Nissan Key Replacement Prices
What’s The Fuss About Automobile Locksmiths? automobile locksmiths near me
Levitra price: Vardenafil online prescription – buy Levitra over the counter
What’s up Dear, are you truly visiting this website on a regular basis,
if so after that you will definitely get good know-how.
5 Killer Quora Answers To Asbestos Lawsuit asbestos
Having read this I thought it was extremely informative.
I appreciate you spending some time and energy
to put this information together. I once again find myself spending
way too much time both reading and commenting. But so what, it was still worth
it!
4 Dirty Little Secrets About The Kids Bunk Bed Industry sale bunk beds