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
Real-time workout monitoring guides your workouts.
Here is my web page – fitness equipment southern california
buy cipro online ciprofloxacin generic price ciprofloxacin order online
Ten 18 Wheeler Accident Lawyers That Really Change Your Life 18 wheeler accident lawyers (Veta)
A Full Gym Experience Anywhere, Whenever.
Also visit my site: fitness equipment stores
https://finasteride.store/# buy propecia without rx
Real-time workout tracking guides your exercises.
Also visit my web-site … fitness equipment stores
8 Tips To Increase Your What CSGO Case Has The Best Odds Game prisma case (Rosaline)
the specifications of various different heights エロ 人形(usually comparing 6 or 8 different heights), that website is most likely an Aliexpress/Alibaba seller.
Three Common Reasons Your Auto Lock Smiths Isn’t Working (And What You Can Do To
Fix It) auto locksmith car key replacement, Aleida,
What The Heck What Is Erb’s Palsy Litigation? Erb’s Palsy Lawsuits
30 Inspirational Quotes About Mesothelioma Lawsuit Mesothelioma settlement
purchase cytotec buy cytotec in usa cytotec online
A Complete fitness equipment stores in california Center Experience Anywhere, Whenever.
The 10 Most Scariest Things About Treatments For Adhd In Adults treatments For adhd in adults, http://www.redsea.gov.eg,
Real-time exercise monitoring guides your exercises.
Here is my blog post – fitness equipment for sale
A Complete Fitness Center Experience Anywhere, At Any Moment.
my blog post :: Bookmarks
tamoxifen headache cost of tamoxifen does tamoxifen cause menopause
The Most Successful Asbestos Settlement Gurus Do 3 Things asbestos law
A Complete Fitness Center Experience Anywhere, At Any Time.
my homepage Bookmarks
cytotec abortion pill: buy misoprostol over the counter – order cytotec online
Grazie all’utilizzo di strumenti specifici e all’adozione di una corretta etica
online, sarà possibile trovare la soluzione giusta per soddisfare i propri
bisogni in totale sicurezza e rispetto della privacy altrui.
Here is my web page – https://igram.world/it/story-saver
Real-time exercise monitoring guides your workouts.
Look at my page: mike's fitness equipment riverside california
I think the admin of this web page is actually working hard for his web
site, as here every material is quality based information.
I’m really loving the theme/design of your site. Do you ever run into
any browser compatibility problems? A few of my blog audience have complained about my site not working correctly in Explorer but
looks great in Chrome. Do you have any suggestions to help fix this issue?
my web-site :: zapakita01
alternatives to tamoxifen: tamoxifen lawsuit – tamoxifen hair loss
https://infomagaz.xyz/
http://ciprofloxacin.tech/# cipro
http://nolvadex.life/# nolvadex d
Real-time workout tracking overviews your exercises.
My homepage – fitness equipment in amarillo texas
9 Things Your Parents Taught You About Dryer Washer Combo
washing machines Washer