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
http://cialist.pro/# buy cialis pill
https://cialist.pro/# Cheap Cialis
Kamagra 100mg buy kamagra online Kamagra tablets
Buy Vardenafil 20mg online: Levitra 20mg price – Buy Vardenafil online
cheapest viagra Order Viagra 50 mg online Viagra online price
11 Strategies To Refresh Your Black Fridge Freezer best american fridge freezer uk
Hello it’s me, I am also visiting this site daily, this site
is really fastidious and the people are actually sharing
pleasant thoughts.
Also visit my web site zapakita01
10 Tell-Tale Signals You Need To Look For A New Upvc Windows
And Doors Upvc windows repair near Me
You’ll Never Be Able To Figure Out This Treadmill
Folded Up’s Secrets Treadmill Folded Up
10 Things You Learned In Kindergarden That’ll
Help You With Avon Shop With My Rep Login Avon Shop With My Rep Login
10 Things We Were Hate About Marc Jacobs Hand Bags marc jacobs bag strap;
Marti,
over the counter sildenafil: Buy generic 100mg Viagra online – Order Viagra 50 mg online
15 Undeniable Reasons To Love Repair Upvc Window upvc window repairs [http://vesti46.ru/user/dugoutdouble2]
I’m really impressed with your writing skills and also with the layout on your blog. Is this a paid theme or did you customize it yourself? Anyway keep up the nice quality writing, it?s rare to see a great blog like this one today..
How To Explain Birth Defect Lawsuit To A Five-Year-Old birth defect Lawsuits
Purchase Cenforce Online: cenforce.pro – Cenforce 150 mg online
Hi this is kind of of off topic but I was wondering if blogs use
WYSIWYG editors or if you have to manually code
with HTML. I’m starting a blog soon but have no coding expertise so I wanted to get
guidance from someone with experience. Any help would be greatly
appreciated!
Purchase Cenforce Online: Purchase Cenforce Online – Cenforce 150 mg online
14 Misconceptions Common To Are CSGO Cases Predetermined huntsman weapon case (minecraftcommand.science)
You’ll Never Be Able To Figure Out This Double Glazing Repair Leeds’s Tricks double glazing repair leeds (Lashunda)
You’ll Never Guess This Window Repair Near Me’s Secrets Window Repair
Cheap Levitra online: Buy generic Levitra online – Buy Vardenafil online
https://kamagra.win/# Kamagra Oral Jelly
ラブドール 等身 大 ダッチワイフについてのセックススペシャリストとのミーティング男性が女性の胸に惹かれる3つの理由カップルの性生活からよりインスピレーションを得るには、カジュアルなフックアップとダッチワイフのどちらが良いですか?
п»їcialis generic Buy Tadalafil 20mg Buy Tadalafil 5mg
https://cenforce.pro/# cheapest cenforce
I think this is one of the most important info for me. And i am glad reading your article. But wanna remark on few general things, The site style is ideal, the articles is really great : D. Good job, cheers
Cialis 20mg price in USA: Generic Cialis without a doctor prescription – Cialis 20mg price
Here’s An Interesting Fact About Espresso Coffee Maker espresso Coffee Machines
viagra without prescription Buy Viagra online Order Viagra 50 mg online