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
raloxifene vs tamoxifen: tamoxifen 20 mg – tamoxifen and ovarian cancer
buy cytotec in usa Cytotec 200mcg price buy cytotec over the counter
https://ciprofloxacin.tech/# ciprofloxacin generic price
Guide To Garden Wood Burner: The Intermediate Guide Towards Garden Wood Burner garden wood Burner
http://nolvadex.life/# natural alternatives to tamoxifen
Howdy! I know this is kinda off topic but I was wondering
if you knew where I could locate a captcha plugin for my comment form?
I’m using the same blog platform as yours and I’m having trouble finding one?
Thanks a lot!
Ѕіmply wish to say youг aгticle is as astounding.
The clarity to yoսr post is just spectacսlar and thawt i could
suppose you are a professionaⅼ in this subject.
Well along with your permission let me to grasp үour RSS feе to keep ᥙpdated
with impending post. Thank you 1,000,000 and please continu the grаtifying work.
Also ѵіsit my page nirwana88
Why All The Fuss About Boat Accident Lawyers?
Boat Accident Law Firm
Evеryone lοveѕ what you guys tend to be up too.
This sort օօf clever ork and reporting! Keep up the good works ghyѕ I’ve you
guys to our blogroll.
my bⅼog posst – jamuslot
10 Apps To Aid You Manage Your Replacement Windows Prices
windows Replacement
This website was… how do you say it? Relevant!! Finally
I’ve found something which helped me. Many thanks!
11 Creative Methods To Write About American Freezer Fridge american integrated fridge freezer
10 Tell-Tale Signals You Should Know To Look For A
New CSGO Cases Highest Roi Cs20 case (maps.google.Com.sa)
The 10 Most Terrifying Things About Double Glazing
Replacement Window Double glazing replacement Window
cytotec online: buy cytotec pills – order cytotec online
ciprofloxacin mail online: ciprofloxacin mail online – buy ciprofloxacin
The 10 Most Scariest Things About Personal
Injury Attorneys personal injury attorney
The Greatest Sources Of Inspiration Of Replacement Windows Near Me Windows Replacement
10 Things That Your Family Taught You About
Porsche Key Fobs Porsche key
What Is Windows Repair Near Me? And How To Make Use Of It sash
10 Websites To Help You To Become An Expert In Private ADHD
Assessment Cost how much does A private adhd assessment cost
The Reason Top 10 Online Shopping Sites In Uk For
Clothes Is The Obsession Of Everyone In 2023 Vimeo
tamoxifen joint pain nolvadex during cycle tamoxifen moa
buy generic ciprofloxacin: ciprofloxacin generic price – antibiotics cipro
I for all time emailed this weblog post page to all my associates, because if like to read it then my friends will too.
The 12 Most Popular Buy SEO Services Accounts To Follow On Twitter Services Seo
clomid nolvadex: does tamoxifen make you tired – what is tamoxifen used for
http://nolvadex.life/# tamoxifen 20 mg
http://ciprofloxacin.tech/# buy ciprofloxacin over the counter
You’ll Never Guess This Replacement Windows
Near Me’s Secrets replacement windows near me