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
Hello, I think your blog might be having browser compatibility issues.
When I look at your website in Ie, it looks fine but when opening in Internet Explorer, it has
some overlapping. I just wanted to give you a quick heads up!
Other then that, awesome blog!
купить диплом ржд many-diplom77.ru .
Приобретение диплома ПТУ с сокращенной программой обучения в Москве
Приобретение школьного аттестата с официальным упрощенным обучением в Москве
Покупка диплома о среднем полном образовании: как избежать мошенничества?
https://denemebonusuverensiteler.top/# deneme bonusu veren siteler mycbet.com
denemebonusuverensiteler.top
Great article! This is the kind of info that should be shared around the net.
Shame on the seek engines for not positioning this submit upper!
Come on over and seek advice from my site . Thank
you =)
https://ww5.datasydney6d.co/
Пошаговая инструкция по официальной покупке диплома о высшем образовании
This is a good tip particularly to those fresh to
the blogosphere. Simple but very precise information… Many thanks for sharing this one.
A must read article!
Great post! We will be linking to this particularly great article on our site.
Keep up the good writing.
Приобретение школьного аттестата с официальным упрощенным обучением в Москве
I know this web page provides quality based articles
and extra stuff, is there any other web page which presents such things in quality?
https://ww1.angkanetpro.buzz/
When I initially commented I clicked the “Notify me when new comments are added”
checkbox and now each time a comment is added I get four emails
with the same comment. Is there any way you can remove me from that service?
Thanks!
где купить аттестат за 9 класс где купить аттестат за 9 класс .
Hello! I’m at work surfing around your blog from
my new iphone! Just wanted to say I love reading your blog and look forward to all
your posts! Carry on the fantastic work!
Good answers in return of this issue with real arguments and explaining everything concerning that.
It’s hard to come by educated people about this subject, but you sound like you know what you’re talking about!
Thanks
Spot on with this write-up, I honestly believe this amazing site
needs much more attention. I’ll probably be returning to read through more, thanks for the advice!
Официальная покупка диплома вуза с упрощенной программой обучения
denemebonusuverensiteler.top deneme bonusu veren siteler betturkey betturkey.com deneme bonusu veren siteler
What’s The Current Job Market For Asbestos Mesothelioma Professionals Like?
mesothelioma lawyers (Vera)
купить диплом недорого нижний новгород 1russa-diploms.ru .
https://locowin-casino.in/
Легальная покупка диплома о среднем образовании в Москве и регионах
Guide To Best Home Coffee Machine: The Intermediate Guide
Towards Best Home Coffee Machine Best Home Coffee Machine
15 Funny People Working Secretly In Vauxhall Keys new key for vauxhall
vivaro Van; rutelochki.ru,
https://casinositeleri.win/# Canl? Casino Siteleri
deneme bonusu veren siteler betturkey
When I originally commented I clicked the “Notify me when new comments are added”
checkbox and now each time a comment is added I get several emails with the same
comment. Is there any way you can remove me from that service?
Many thanks!
https://ww9.mbahtogel.top/
9 Things Your Parents Taught You About Bifold Repairs Near Me bifold repairs near me
en cok kazand?ran slot oyunlar?: slot tr online – slot tr online