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 Best Tips You’ll Receive About Double Glazing
Repairs Near Me double Glazing repairs near Me
You’ll Never Guess This Car Key Cut Price’s Tricks cheapest
tamoxifen alternatives premenopausal: where to buy nolvadex – low dose tamoxifen
get generic propecia pill cost of generic propecia online buying cheap propecia
Are You Responsible For An Repair Timber Windows Budget?
10 Fascinating Ways To Spend Your Money Double Glazing Repairs Near Me (Valetinowiki.Racing)
The Best Advice You Can Receive About Online Store Uk Cheapest Garden Water Timer
Hello, I wish for to subscribe for this website to
obtain most recent updates, so where can i do it please help out.
Look into my webpage … Beziehungs-Podcast
cost propecia no prescription: propecia without insurance – cost cheap propecia without insurance
10 Places To Find Autowatch Ghost Installers
alarm
generic cialis soft 20mg
10 Things You Learned From Kindergarden That Will Help You
With Upvc Window Repairs window repairs near me
7 Small Changes That Will Make A Big Difference With Your Double Glazing Repairs
North London window lock Repair London
20 Fun Infographics About Robot Vacuums For Pet
Hair Robot Mop Vacuum Combo
Which Is The Best Online Supermarket: 10 Things I’d Like To Have Learned
Earlier Paintable Air Return Grill (Morgan)
zestoretic price: buy zestoretic online – zestril
get generic propecia without rx buy propecia tablets buy propecia prices
5 Killer Quora Answers On Shopping Online Uk Clothes shopping online uk clothes
The Reasons Why Double Glazing Repair Near Me Is The Most Sought-After Topic
In 2023 Double glazing Repair
The Leading Reasons Why People Perform Well At The Treadmills For Sale Industry
treadmills for Sale near Me (125.141.133.9)
http://finasteride.store/# get cheap propecia no prescription
hello!,I really like your writing very so much! proportion we communicate extra approximately
your article on AOL? I need an expert on this area to
solve my problem. May be that is you! Taking a look forward to
look you.
An Easy-To-Follow Guide To How To Buy Clothes Online From Uk Electrical Surge Protector Tlp602 (Ardis)
cost generic propecia online how cЙ‘n i get cheap propecia pills buying cheap propecia online
15 Things You’re Not Sure Of About Motor Vehicle Case motor vehicle accident attorney
ciprofloxacin over the counter: ciprofloxacin 500mg buy online – buy ciprofloxacin over the counter
10 Healthy Habits For Adult Adhd Treatment treatment of adult adhd
buy cipro online without prescription buy cipro online cipro for sale
lisinopril 5mg buy lisinopril 20 mg tabs lisinopril 5 mg uk price
Your mode of telling all in this article is genuinely pleasant, all be able to easily know it,
Thanks a lot.
What’s The Job Market For American-Style Fridge Professionals?
american-style Fridge