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
Подробный обзор Istanbul International Airport, развитие аэропорта на протяжении лет.
Загадки и тайны Istanbul International Airport, о которых вы не знали.
Уникальная архитектура Istanbul International Airport, которые вызывают восхищение.
Планы и развитие Istanbul International Airport, направления роста и совершенствования.
Сервисы и удобства Istanbul International Airport, что сделано для комфорта путешественников.
all istanbul airports all istanbul airports .
Five Killer Quora Answers On Reprogramming Car Key Reprogramming Car Key (Minecraftcommand.Science)
9 Things Your Parents Taught You About Folding Treadmills UK folding treadmills uk, Walker,
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 site to come back
later. Cheers
Wow, this piece of writing is pleasant, my younger sister is analyzing these things, thus I am going to let know her.
You Will Meet You The Steve Jobs Of The Auto Key Repair Industry Auto key repair near me
купить диплом о высшем образовании московская область man-diploms.ru .
купить диплом ростов man-diploms.ru .
Подробный обзор Istanbul International Airport, успешные этапы развития.
Интересные факты о Istanbul International Airport, которые вас удивят.
Особенности проектирования аэропорта, которые поражают воображение.
Что ждет аэропорт в ближайшие годы, новые горизонты и возможности.
Что делает аэропорт уникальным, какие услуги понравятся каждому.
istanbul new international airport https://airportistanbulinternational.com/ .
5 Killer Quora Answers To Fold Away Treadmill UK fold away
Treadmill uk (wx.abcvote.cn)
Интервью после выступления горожан группы «gaina» (неопр.) (31 марта 2008).
Дата обращения: 31 марта 2008.
my web blog; Круизные морские круизы
20 Myths About Seat Replacement Key: Dispelled replacement Seat car key
7 Essential Tips For Making The Most Of Your Repair Upvc
Windows upvc windows repair – snider-potter.hubstack.net –
5 Killer Quora Answers To ADHD Diagnosis Near Me
Adhd Diagnosis Near Me
кассовый чек на аванс и зачет аванса
Medicines information sheet. Brand names.
generic amlodipine manufacturers
Everything news about meds. Get information here.
Thanks for another informative web site. The place else may just I am
getting that kind of information written in such an ideal means?
I’ve a project that I am simply now working on,
and I’ve been on the look out for such info.
Peculiar article, exactly what I was looking for.
If you are going for best contents like me, simply visit
this website daily for the reason that it presents quality contents, thanks
Hey there would you mind sharing which blog platform you’re using?
I’m planning to start my own blog in the near future
but I’m having a hard time choosing between BlogEngine/Wordpress/B2evolution and
Drupal. The reason I ask is because your design and style seems different then most blogs and I’m looking for something unique.
P.S My apologies for getting off-topic but I had to ask!
How Spend Money On And Sell Bank Bulk Reo And Npn 다바오 홀덤 하는법
How To Choose The Right Locksmiths On The Internet Locksmiths Near Me Prices
(King-Wifi.Win)
купить диплом капитана landik-diploms.ru .
Подробный обзор Istanbul International Airport, как все начиналось и что из этого вышло.
Интересные факты о Istanbul International Airport, которые вас удивят.
Уникальная архитектура Istanbul International Airport, которые поражают воображение.
Планы и развитие Istanbul International Airport, новые горизонты и возможности.
Какие новшества ждут пассажиров, на что стоит обратить внимание.
istanbul intl airport https://airportistanbulinternational.com/ .
купить диплом специалиста в москве купить диплом специалиста в москве .
15 Pinterest Boards That Are The Best Of
All Time About Private Adhd Assessment London how to get a Adhd Assessment
15 Interesting Hobbies That Will Make You More Effective At
Asbestosis Asbestos Mesothelioma Attorney mesothelioma lawyers
The Most Successful Samsung Freezer Experts Have Been Doing Three Things
Best Samsung Fridge Freezer
Thank you for the auspicious writeup. It in fact was a amusement account it.
Look advanced to more added agreeable from you!
However, how could we communicate?
Attractive section of content. I just stumbled upon your blog and in accession capital
to assert that I acquire actually enjoyed account your blog posts.
Any way I will be subscribing to your augment and even I achievement you access
consistently rapidly.