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
I’m curious to find out what blog system you happen to be utilizing?
I’m having some minor security issues with my latest website and I’d like to find something more safe.
Do you have any solutions?
My web site … nerve fresh reviews
rybelsus pill rybelsus price buy semaglutide online
What’s up, after reading this awesome piece of writing i am as well glad to share my familiarity
here with colleagues.
The No. Question Everybody Working In Lexus Key Replacement Near Me Needs To
Know How To Answer Lexus car key; https://barbour-downs.mdwrite.net/,
Ozempic without insurance: buy cheap ozempic – buy ozempic
Upvc Half Door Panels Techniques To Simplify Your Daily
Lifethe One Upvc Half Door Panels Trick Every Individual Should Know How to remove a upvc Door panel
buy ozempic pills online: Ozempic without insurance – ozempic
The Reason Behind Volvo Key Replacement Is Everyone’s Passion In 2023 volvo
truck key blank (Lashonda)
It’s a shame you don’t have a donate button! I’d most certainly donate to this outstanding blog!
I guess for now i’ll settle for book-marking and adding your RSS feed to my Google account.
I look forward to brand new updates and will talk about this website with my Facebook group.
Talk soon!
ozempic cost: ozempic online – ozempic coupon
Wow, this article is nice, my sister is analyzing such
things, thus I am going to inform her.
Профессиональный сервисный центр по ремонту автомагнитол в Москве.
Мы предлагаем: ремонт автомагнитолы
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
buy rybelsus online: semaglutide cost – buy semaglutide online
After implementing your Web 2.0 sites for backlinks strategy, it’s crucial to monitor the performance of your backlinks. Use tools like Google Analytics or Ahrefs to track referral traffic, and assess how your backlinks are influencing your site’s SEO. Regularly evaluating your strategy allows you to make adjustments and optimize your efforts.
10 Quick Tips About Pragmatic 라이브 카지노
buy semaglutide online: cheapest rybelsus pills – rybelsus price
https://ozempic.art/# ozempic coupon
10 No-Fuss Methods For Figuring The Pragmatic Free Slot Buff You’re Looking For 프라그마틱
semaglutide online semaglutide cost semaglutide online
The 9 Things Your Parents Taught You About Bioethanol Fire ethanol
fire; Ruben,
5 Killer Quora Answers On Situs Terpercaya Situs Terpercaya
Really all kinds of fantastic material!
Feel free to visit my website :: https://mostbet-bk.com
Wow, superb blog format! How long have you ever been running a blog for?
you make running a blog look easy. The full look of your website is magnificent, as smartly
as the content material!
My page; tonic greens customer reviews
Diosa brought me to the CrashPad. I was drawn to the revolutionary act of queer/trans
POC making queer porn for other queer and trans POC.
Excellent pieces. Keep writing such kind of information on your site.
Im really impressed by your site.
Hello there, You’ve performed an excellent job. I will definitely digg
it and in my view recommend to my friends. I am confident
they will be benefited from this site.
Hello! This is kind of off topic but I need some
help from an established blog. Is it hard to set
up your own blog? I’m not very techincal but I can figure things out
pretty quick. I’m thinking about setting up my own but I’m not
sure where to start. Do you have any ideas or suggestions?
Thanks
ozempic generic: ozempic cost – ozempic coupon
web tasarım
Wow, that’s what I was seeking for, what a information! present here at
this website, thanks admin of this website.
buy ozempic pills online: ozempic cost – ozempic coupon
Hi there, all is going nicely here and ofcourse every one is sharing information,
that’s really excellent, keep up writing.