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
Are You Responsible For An Upvc Window Repairs Budget?
10 Incredible Ways To Spend Your Money window repairs near Me
Double Glazing Firms Near Me: 11 Things You’re Not Doing Double glazed Unit replacement
Wood Burning Stove Small Tools To Make Your Daily Lifethe
One Wood Burning Stove Small Trick Every Person Should Be Able
To wood burning stove small
Thanks for sharing your thoughts about проект домашнего кинотеатра.
Regards
5 Killer Quora Answers On Auto Accident Attorneys auto Accident attorneys
You’ll Never Guess This Upvc Door Repairs Near Me’s Tricks upvc door repairs Near Me
I like it whenever people come together and share views.
Great website, stick with it!
10 Mesothelioma And Lawyers Projects Related To Mesothelioma And Lawyers To Extend Your Creativity Affordable Mesothelioma Attorney
https://nolvadex.life/# pct nolvadex
lisinopril tablets: lisinopril 5mg prices – lisinopril from mexico
casino bono de bienvenida sin deposito peru https://sin-deposito-peru.org/ .
It’s The Good And Bad About Designer Handbags Designer Handbags Pink [Bookmarkingworld.Review]
does tamoxifen cause joint pain: tamoxifen cost – nolvadex 20mg
You’ll Be Unable To Guess Nespresso Magimix Coffee Machine’s Tricks
Nespresso Magimix Coffee Machine
14 Common Misconceptions Concerning Injury Law
injury law firm
Affordable SEO: The Good, The Bad, And The Ugly affordable seo Company Near me
9 . What Your Parents Taught You About Upvc Window Repairs upvc Window Repairs
A Look At The Ugly Facts About CS GO Case Website case spectrum (Hunter)
bono gratis para casino sin deposito peru https://sin-deposito-peru.org/ .
Why You Should Focus On Enhancing Double Glazed Window Repair replacement
Hi, Neat post. There’s a problem along with your web site in web explorer, would
check this? IE still is the market chief and a big portion of other folks will miss
your magnificent writing because of this problem.
prednisone daily
10 Myths Your Boss Is Spreading About Birth Injury Attorneys birth injury Lawsuit
You’ll Never Guess This Treadmill Sale UK’s Tricks Treadmill Sale Uk – Ilovebookmarking.Com,
Do you mind if I quote a few of your posts as long as I provide credit and sources
back to your site? My blog site is in the very same niche as yours and my visitors would truly benefit from a lot of the information you provide here.
Please let me know if this okay with you.
Many thanks!
The Reasons ADHD Tests Is Harder Than You Imagine adult testing adhd
10 Reasons Why People Hate Unlock Car Service.
Unlock Car Service Automotive Lockouts (https://Sciencewiki.Science/Wiki/11_Ways_To_Completely_Redesign_Your_Lockout_Car)
What Is The Reason? Audi Battery Key Replacement
Is Fast Becoming The Hot Trend For 2023 Replacement Audi Key Cost Uk
It’s in point of fact a great and helpful piece of information. I’m satisfied that you shared this helpful
info with us. Please keep us informed like this. Thank you for sharing.
20 Resources That Will Make You Better At Mesothelioma
Law Asbestos legal