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
Have you ever considered publishing an ebook or guest authoring on other sites?
I have a blog centered on the same topics you discuss and would love to have you share some stories/information. I know
my readers would enjoy your work. If you are even remotely interested, feel free
to send me an e mail.
Diagnostic Check Near Me Tools To Simplify Your Daily Life vehicle
opros.flybb.ru/viewtopic.php?f=2&t=660
Everything is very open with a very clear description of the issues.
It was definitely informative. Your website is useful. Thanks for sharing!
pharmacie en ligne france livraison internationale: pharmacie en ligne sans ordonnance – п»їpharmacie en ligne france
My programmer is trying to convince me to move to .net from
PHP. I have always disliked the idea because of the costs.
But he’s tryiong none the less. I’ve been using Movable-type on a variety of websites for about a year and am nervous about switching to
another platform. I have heard good things about blogengine.net.
Is there a way I can import all my wordpress content into it?
Any kind of help would be greatly appreciated!
Профессиональный сервисный центр по ремонту плоттеров в Москве.
Мы предлагаем: сервис по ремонту плоттеров
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
Hi there it’s me, I am also visiting this website on a regular basis, this web site
is in fact pleasant and the viewers are actually sharing nice thoughts.
Guide To Cheap Rabbit Vibrators: The Intermediate Guide Towards Cheap
Rabbit Vibrators cheap Rabbit Vibrators
Wow, fantastic blog structure! How long have you ever been blogging for?
you made blogging look easy. The entire look of your web
site is magnificent, let alone the content!
you are actually a good webmaster. The site loading pace is amazing.
It kind of feels that you are doing any distinctive trick.
Moreover, The contents are masterpiece. you have done a fantastic task on this topic!
Pharmacie sans ordonnance: Acheter Cialis – acheter mГ©dicament en ligne sans ordonnance
http://vgrsansordonnance.com/# Viagra gГ©nГ©rique sans ordonnance en pharmacie
http://clssansordonnance.icu/# pharmacie en ligne france livraison internationale
It’s Time To Expand Your Eco-Friendly Scooters Options electric Mobility scooter cheap
Prix du Viagra en pharmacie en France Viagra prix Viagra femme sans ordonnance 24h
Профессиональный сервисный центр по ремонту МФУ в Москве.
Мы предлагаем: где отремонтировать мфу в москве
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
pharmacie en ligne livraison europe: pharmacie en ligne sans ordonnance – pharmacie en ligne france livraison belgique
Thank you for the auspicious writeup. It in fact was a amusement
account it. Look advanced to more added agreeable from you!
However, how can we communicate? http://Josephpesco.info/qaz/index.php/User:NaomiYoder35
pharmacie en ligne france livraison belgique: Cialis sans ordonnance pas cher – pharmacie en ligne france livraison belgique
Your means of telling everything in this article is genuinely
good, every one be able to effortlessly understand it, Thanks a lot https://vegtech.ca/en/contact/
It is perfect time to make some plans for the future and it is
time to be happy. I’ve read this post and if I could I desire to suggest
you some interesting things or tips. Perhaps you could write next articles referring to this article.
I desire to read more things about it!
Строительство загородных домов под ключ – это процесс возведения дома с нуля до полной готовности, включая все этапы проектирования, строительства и
отделки, без участия заказчика.
кладка газобетона 375 цена
This piece of writing is genuinely a pleasant one it assists new web visitors, who are
wishing for blogging.
Viagra sans ordonnance livraison 24h: Acheter du Viagra sans ordonnance – Prix du Viagra en pharmacie en France
Профессиональный сервисный центр по ремонту принтеров в Москве.
Мы предлагаем: починка принтера на дому
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
https://clssansordonnance.icu/# pharmacie en ligne livraison europe
п»їpharmacie en ligne france: Cialis sans ordonnance pas cher – acheter mГ©dicament en ligne sans ordonnance
vente de mГ©dicament en ligne: pharmacie en ligne pas cher – Pharmacie sans ordonnance
Thank you for the good writeup. It in fact was a amusement
account it. Look advanced to far added agreeable from you!
However, how could we communicate?