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
We are a group of volunteers and opening a
new scheme in our community. Your web site offered us with valuable information to work on. You have done a formidable job and our entire community will
be grateful to you.
The People Closest To Upvc Door Repair Near Me Share Some Big Secrets upvc door repair cost; Karin,
deneme bonusu veren siteler mycbet.com http://ultrabet-tr.online/# ultrabet guncel
ultrabet yeni giris 1125 ultrabet tr online ultrabet tr online
casino ohne einzahlung bietet eine spannende und bequeme Möglichkeit, Ihre Lieblingsspiele bequem von zu Hause aus zu genießen. Mit einer großen Auswahl an Optionen können Sie jederzeit und überall Casino-Klassiker wie Poker, Blackjack und Spielautomaten spielen. Online-Casinos bieten ein spannendes Spielerlebnis mit realistischer Grafik und Live-Dealer-Optionen. Tauchen Sie noch heute in die Welt der online casinos ein und erleben Sie die Aufregung und die Vorteile des virtuellen Spielens Ihrer Lieblingsspiele.
Good way of describing, and pleasant article to take information concerning my presentation topic,
which i am going to present in school.
сервис накрутки подписчиков в телеграм сервис накрутки подписчиков в телеграм .
сервис накрутки подписчиков instagram сервис накрутки подписчиков instagram .
Hey there! This is kind of off topic but I need some advice from an established blog.
Is it difficult to set up your own blog?
I’m not very techincal but I can figure things
out pretty fast. I’m thinking about creating my own but I’m not sure
where to begin. Do you have any ideas or suggestions? With thanks
Canl? Casino Siteleri: Casino Siteleri – Deneme Bonusu Veren Siteler
Тут можно преобрести сейфы для оружия пистолетные оружейный сейф купить
deneme bonusu veren siteler mycbet.com: deneme bonusu veren siteler betturkey – deneme bonusu veren siteler mycbet.com
matadorbet matadorbet giris matadorbet bid
Тут можно преобрести оружейный сейф на заказ москва купить оружейный сейф для пистолета
Тут можно преобрести купить оружейный шкаф купить сейф для охотничьего карабина
No matter if some one searches for his essential thing,
thus he/she desires to be available that in detail,
thus that thing is maintained over here.
Тут можно преобрести оружейные шкафы и сейфы сейф оружейный цена
deneme bonusu veren siteler denemebonusu2026.com: deneme bonusu veren siteler yeni – deneme bonusu veren siteler yeni
Nicely put, Kudos.
Greetings! Quick question that’s totally off topic.
Do you know how to make your site mobile friendly?
My site looks weird when browsing from my apple iphone.
I’m trying to find a template or plugin that might be able to fix this
issue. If you have any suggestions, please share. Thank you!
https://slot-tr.online/# slot tr online
deneme bonusu veren siteler
10 Meetups On Coffee Machines Sage You Should Attend coffee Machines Uk
5 Laws That’ll Help The Double Glazing Condensation Repair Kit Industry double glazed repairs
сервис для накрутки подписчиков в инстаграме сервис для накрутки подписчиков в инстаграме .
Way cool! Some extremely valid points! I appreciate you writing this post
and also the rest of the website is really good.
slot oyunlar? slot oyunlar? slot oyunlar?
How To Become A Prosperous Glassrepair Even If You’re Not Business-Savvy double
What You Should Be Focusing On Making Improvements To Pragmatic Site 프라그마틱 슬롯버프
buy generic furosemide online
poznaj fakty tutaj i teraz