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
20 Fun Facts About Affordable SEO Services Near Me affordable seo consultant (Candace)
5 Upvc Windows And Doors Near Me Tips From The Pros
upvc Windows Repair near Me
can i get mentat without rx
can you get valtrex no prescription
A片
Having read this I thought it was extremely informative.
I appreciate you finding the time and energy to put this content together.
I once again find myself personally spending a significant amount of
time both reading and posting comments. But so
what, it was still worth it!
10 Unquestionable Reasons People Hate Seo Search Engine Optimisation Search Engine optimization Packages [maps.google.dm]
Всем привет! Может кто знает, где почитатьполезные блоги о недвижимости? Сейчас читаю https://universal37.ru
An Intermediate Guide To Repair Upvc Window Window Repair
The 10 Most Scariest Things About Best Single Stroller
2024 best Single stroller 2024
Hi, I do believe this is a great website. I stumbledupon it 😉 I’m going to return yet again since I book-marked it.
Money and freedom is the greatest way to change, may you be
rich and continue to help others.
Hello! can i order cheap finasteride online excellent website.
I loved as much as you’ll receive carried out right here.
The sketch is tasteful, your authored material stylish.
nonetheless, you command get bought an shakiness over that you wish
be delivering the following. unwell unquestionably come more formerly again as exactly the same nearly very often inside case you
shield this hike.
Feel free to visit my blog :: the genius wave reviews
can i purchase cheap feldene pill
Ten 18 Wheeler Accident Law Firms That Really Help You Live Better 18 wheeler Accident law firms
cost cheap cabgolin prices
I am truly glad to read this weblog posts which contains tons of helpful data, thanks for providing these
data.
5 Killer Quora Answers To Popular Casino Slots popular casino slots (Ricky)
10 Great Books On Replacement Upvc Window Handles upvc window repair near me
how much is lisinopril 40 mg
can you buy generic promethazine price
After checking out a handful of the blog articles on your blog, I
really like your way of writing a blog. I added it to my bookmark webpage list and will be checking back soon. Take a
look at my web site too and tell me what you think.
Feel free to surf to my web-site – boostaro price
Why Dangerous Drugs Isn’t As Easy As You Imagine Dangerous drugs Lawyers
Need Inspiration? Check Out Drive Folding Mobility Scooter scooter folds (Madelaine)
how to buy cheap lexapro without insurance
Introduction: Chill Cooler Portable AC air
conditioning units have become increasingly popular for individuals seeking comfort in various settings, including offices,
bedrooms, and outdoor spaces.
I’m gone to convey my little brother, that he should also visit this weblog on regular basis
to get updated from latest information.
I every time emailed this weblog post page to all my friends, for the
reason that if like to read it next my friends will too.
9 Lessons Your Parents Teach You About Slot Tournaments Slot Tournaments
The 10 Most Terrifying Things About Truck Accident Law Firm truck accident law firm
20 Trailblazers Lead The Way In Slot Updates Classic slots (http://www.rogeryamashita.com)