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
The No. 1 Question Everyone Working In Asbestos Mesothelioma Lawsuit Should Be Able To Answer mesothelioma Attorneys
Hey there! This post couldn’t be written any better!
Reading this post reminds me of my previous room mate!
He always kept talking about this. I will forward this post to him.
Fairly certain he will have a good read. Thank you for sharing!
Hi there! I’m at work surfing around your blog from my new apple iphone!
Just wanted to say I love reading your blog and look forward to
all your posts! Carry on the outstanding work!
priligy maxpharm: priligy max pharm – dapoxetine online
Inspiring quest there. What occurred after?
Take care!
buy amoxicillin online with paypal http://priligymaxpharm.com/# buy dapoxetine online
amoxicillin 500mg capsule https://amoxilcompharm.com/# generic amoxil 500 mg
WOW just what I was looking for. Came here by searching for strong cardboard boxes
lgbt porn
заказать кухню — Сделайте заказ на кухню своей мечты с доставкой.
amoxicillin where to get https://prednisoneraypharm.com/# generic prednisone pills
I’m not that much of a online reader to be honest but your sites really nice,
keep it up! I’ll go ahead and bookmark your website to
come back later. Cheers
Где и как купить диплом о высшем образовании без лишних рисков
Официальная покупка диплома вуза с сокращенной программой обучения в Москве
priligy: priligy – priligy maxpharm
Тут можно преобрести купить сейф под оружие оружейный сейф на заказ москва
15 Best Accident Lawyer Near Me Bloggers You Should Follow Best accident lawyer near me
где-то потребуется профильная подготовка (например, http://demo.4admins.ru/viewtopic.php?f=10&t=2160 диплом педагогического вуза).
buy amoxicillin 500mg canada amoxicillin 500mg capsules antibiotic amoxicillin online no prescription
priligy max pharm: dapoxetine price – Priligy tablets
Как правильно приобрести диплом колледжа или ПТУ в России, важные моменты
priligy Priligy tablets priligy max pharm
where to buy cheap clomid online: clomid online – cost of clomid without prescription
amoxicillin tablet 500mg https://clomidrexpharm.com/# cost of generic clomid pills
Guide To Repair Upvc Window: The Intermediate Guide The Steps To Repair Upvc Window upvc
Medicines information sheet. What side effects?
where to buy setraline
All what you want to know about drugs. Read information now.
can i order clomid for sale clomid purchase online rex pharm can you get clomid for sale
amoxicillin discount coupon https://clomidrexpharm.com/# can i buy clomid without insurance
amoxicillin 875 125 mg tab https://priligymaxpharm.com/# priligy max pharm
cost of cheap clomid pills: cheap clomid – generic clomid