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://ozempic.art/# ozempic cost
Great weblog right here! Additionally your site rather a lot up fast!
What host are you using? Can I am getting your affiliate link in your host?
I want my site loaded up as fast as yours lol
Профессиональный сервисный центр ремонт андроид телефонов ближайший ремонт сотовых
Five Car Lock Smith Lessons From The Pros car keys locksmith near me
Watch This: How Integrated Fridge Freezer Is Gaining Ground And How To Respond tall integrated fridge freezer 50 50
Thanks for your marvelous posting! I genuinely enjoyed reading it, you will be a great author.I
will ensure that I bookmark your blog and may come back in the future.
I want to encourage that you continue your great work, have a nice weekend!
What’s The Current Job Market For Modular Sleeper Sofa Professionals Like?modular sleeper Sofa
Guide To Best Retro Fridge Freezer: The Intermediate Guide Towards Best Retro Fridge Freezer best retro fridge freezer
нарколог на дом вывод из запоя на дому нарколог на дом вывод из запоя на дому .
12 Stats About Situs 4d To Make You Seek Out Other People toto online terbaik (Marlon)
10 Things That Your Family Taught You About Upvc Windows And Doors Upvc Window
It’s impressive that you are getting ideas from this paragraph as well as from our argument made at this time.
cheapest rybelsus pills rybelsus pill rybelsus coupon
14 Questions You Might Be Refused To Ask Peugeot Boxer Key keys
What’s Happening i am new to this, I stumbled upon this I’ve
found It absolutely helpful and it has helped me out loads.
I’m hoping to give a contribution & aid other users like its aided
me. Good job.
The provided substance of this blog piece is highly intriguing .
I enjoyed the way you scrutinized the various issues so extensively
and distinctly . You facilitated me procure fresh perspectives
that I never considered before. I appreciate for sharing your
mastery and competence – it has enabled me to improve myself further
.
I particularly liked the innovative viewpoints you introduced , which enlarged my mindset
and reasoning in significant directions . This blog is systematic
and enthralling, which is essential for information of this level .
I hope to review additional of your compositions in the days
to come , as I’m confident it is sure to continue to be informative and
assist me continue growing . I express my gratitude !
Also visit my web blog; advantages of playing at mobile casinos
See What Treat Generalized Anxiety Disorder Tricks The Celebs Are Using Treat Generalized Anxiety Disorder
What’s The Current Job Market For Coffee Machines Capsules Professionals?coffee Machines capsules
11 “Faux Pas” That Are Actually Okay To Create With Your Pragmatic Free 프라그마틱 사이트
Sweet blog! I found it while searching on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News?
I’ve been trying for a while but I never seem to get there!
Thank you
rybelsus pill rybelsus cost rybelsus price
See What Kids Beds Bunk Beds Tricks The Celebs Are Using kids beds bunk
Why Land Rover Replacement Keys Isn’t A Topic That People Are Interested In Land Rover Replacement Keys land Rover key cutting And programming
https://ozempic.art/# ozempic coupon
В магазине сейфов предлагают купить сейф в москве купить сейф москва
Great work! This is the type of information that are supposed to be shared across the web.
Disgrace on Google for not positioning this publish upper!
Come on over and consult with my site . Thank you =)
buy rybelsus online rybelsus cost rybelsus pill
перейти на сайт Kraken19.at
I do agree with all the concepts you have introduced
to your post. They’re really convincing and will certainly work.
Nonetheless, the posts are too brief for starters.
May just you please extend them a little from subsequent
time? Thanks for the post.