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
Coffee Machine Explained In Fewer Than 140 Characters Barista machines
15 Startling Facts About Defra Wood Burning Stoves UK You’ve Never Known DEFRA stoves near me (morphomics.science)
We are a group of volunteers and starting a
new scheme in our community. Your website provided us with valuable info
to work on. You’ve done an impressive job and our entire community will be thankful to you.
It’s Time To Expand Your Mesothelioma Settlement Options Marion mesothelioma Lawyer
Attractive section of content. I just stumbled upon your weblog and
in accession capital to assert that I get actually enjoyed account your blog posts.
Anyway I’ll be subscribing to your augment and even I achievement you access
consistently quickly.
20 Resources That Will Make You Better At Shopping Online Uk Clothes Online Shopping
What’s The Job Market For Private Psychiatrist Newcastle Professionals?
Private psychiatrist newcastle
Five Killer Quora Answers To Double Glazing Repairs Leeds double glazing repairs leeds (Breanna)
buy nitroglycerin without a prescription – nitroglycerin ca diovan 80mg canada
Рекомендую надежную Букмекерскую контору 1win
super kamagra: kamagra oral jelly – buy Kamagra
20 Things That Only The Most Devoted New Slots Online Fans Understand
Slots with bonuses
Levitra tablet price levitrav.store Buy generic Levitra online
Online Shopping Website In London Techniques To Simplify Your Everyday Lifethe Only
Online Shopping Website In London Trick That Every Person Must Be Able To online shopping Website in london
Заказать SEO продвижение сайтов https://seoshnikigo.ru/ в ТОП поисковых систем Яндекс и Google в Москве, оплата за результат и по факту. Кейсы, стратегии продвижения, скидки и акции, индивидуальный подход
http://levitrav.store/# Cheap Levitra online
https://cialist.pro/# Buy Cialis online
What’s The Job Market For Mercedes Replacement Key Cost Uk Professionals Like?
key
Five Things You Don’t Know About Ford Car Key Ford Transit Keys
https://cialist.pro/# Generic Cialis price
http://cialist.pro/# Buy Cialis online
10 Things People Hate About Replacement Volvo Keys spare Volvo key
5 Qualities People Are Looking For In Every Bentley Bentayga
Key bentley continental Gt Key
Kamagra 100mg price: kamagra.win – Kamagra Oral Jelly
This Week’s Top Stories About How To Get CS GO Cases How To Get CS GO Cases kilowatt Case
How Do I Explain Marc Jacobs Bag – Black To
A 5-Year-Old marc jacobs bag Shoulder
11 Ways To Completely Sabotage Your List Of Online
Shopping Sites In Uk Office Artificial Plant Decor (Shari)
Viagra without a doctor prescription Canada Cheapest place to buy Viagra Viagra Tablet price
http://viagras.online/# generic sildenafil
https://levitrav.store/# Levitra online pharmacy