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
Sweet blog! I found it while searching on Yahoo News.
Do you have any suggestions on how to get listed in Yahoo News?
I’ve been trying for a while but I never seem to get there!
Cheers
prednisone price: buy prednisone – prednisone 10mg for sale
5 The 5 Reasons Bmw Smart Key Is Actually A Great Thing diamond Button on bmw Key
Meds prescribing information. Generic Name.
where to buy generic ceftin pill
Actual information about drug. Read information now.
What’s up, after reading this remarkable post i am too delighted to share my know-how here with
friends.
priligy: dapoxetine online – Priligy tablets
You actually make it seem so easy with your presentation but I find
this topic to be really something that I think I would never understand.
It seems too complicated and very broad for me. I’m looking forward for your next post, I’ll try to get the hang of it!
darknet drug links dark web drug marketplace https://darknetmarketstore.com/ – darknet links
prednisone 30: prednisoneraypharm – prednisone 10mg tablet price
9 . What Your Parents Teach You About Twin Pushchairs twin pushchairs
max pharm: max pharm – buy priligy
prednisone 20 mg tablets: ray pharm – prednisone 5mg daily
prednisone coupon: prednisone – prednisone 30 mg tablet
apo prednisone: ray pharm – buy prednisone 20mg
buy priligy max pharm: priligy maxpharm – dapoxetine price
generic for amoxicillin: buy amoxil online – amoxicillin 500mg capsule buy online
dark web site darknet drugs https://darknetmarketstore.com/ – dark web search engine
Drugs prescribing information. Brand names.
is hydroxyzine like promethazine
Everything news about medicine. Get now.
I know this website presents quality based posts and other stuff, is there any other web site which presents such data in quality?
It’s an remarkable post in support of all the web users;
they will take advantage from it I am sure.
order generic clomid tablets: cheap clomid – can i order generic clomid without insurance
respectively regardless of the learning style of your child or his busy
schedule, you for sure discover a https://www.oursupporthub.co.uk/india/psh-businesses/edward-evans that
is real for you suitable.
jack poker отзывы
jack poker реальные отзывы
dapoxetine price: priligy maxpharm – priligy maxpharm
priligy max pharm: dapoxetine online – priligy
Der “RealPlayer Downloader” bindet sich in verschiedene Browser ein und ermöglicht
es, kostenlos Videos aus dem Netz zu laden.
Excellent beat ! I wish to apprentice while you amend your website, how can i
subscribe for a blog website? The account helped me
a acceptable deal. I had been a little bit acquainted of this your broadcast offered bright clear
idea
prednisone 60 mg: prednisoneraypharm – prednisone 1 mg tablet
Every weekend i used to pay a quick visit this website, as i
wish for enjoyment, since this this web site conations truly fastidious funny information too.