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
https://lisinopril.network/# lisinopril tabs 4mg
10 Key Factors Regarding Coffee Machine With Pods You Didn’t Learn At School Office Coffee machines
наркологический центр в алматы https://someblog.ru/
A How-To Guide For Automobile Locksmith From Beginning To End Automobile Locksmiths
prinivil price: medication lisinopril 5 mg – lisinopril 1 mg
You actually reported that very well!
10 Easy Steps To Start Your Own Personal Injury Settlement Business personal injury lawsuit
See What Link Alternatif Gotogel Tricks The Celebs Are Using link alternatif Gotogel
15 Reasons Not To Ignore Work From Home Jobs Online Uk Work from home jobs no experience uk
20 Trailblazers Are Leading The Way In Double Glazing Fitters Near Me window
cost cheap propecia prices: cost of propecia pill – buy propecia for sale
cipro online no prescription in the usa: cipro 500mg best prices – ciprofloxacin order online
You’ll Never Guess This Window Repair Near Me’s Tricks window repair near me
Responsible For A Personal Injury Firm Near Me Budget?
12 Top Ways To Spend Your Money personal injury lawyers houston
You need to bbе a paгt of a contest for one
of the most useful blogs оn the web. I most cerfainly wiⅼl recommend this web site!
my blog post :: harga pagar panel beton
ciprofloxacin mail online: buy cipro – buy ciprofloxacin over the counter
Regards, I appreciate this!
https://lisinopril.network/# lisinopril otc
https://finasteride.store/# cost of generic propecia without insurance
lisinopril 5 mg tabs: where can i buy zestril – lisinopril price in india
cytotec online: cytotec online – order cytotec online
https://finasteride.store/# get propecia tablets
10 Things Everyone Hates About Counter Strike
Global Offensive Ps4 cs2 case opening (http://www.Dermandar.com)
Lost My Car Keys No Spare Tools To Make Your Everyday Lifethe Only Lost My Car Keys No Spare Trick That Every Person Must Know Lost My Car Keys No Spare, https://Www.Cheaperseeker.Com,
Ten Reasons To Hate People Who Can’t Be Disproved Best Slot Payouts best slot machines
buy cytotec pills online cheap: buy cytotec over the counter – order cytotec online
WOW just what I was looking for. Came here
by searching for Deck Builder
https://nolvadex.life/# tamoxifen and bone density
Five Killer Quora Answers To SEO Services Company London seo services company london – Clarita,
lisinopril india: lisinopril brand name in india – lisinopril 20 mg tablet price