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
This was quite informative. More at residential scaffolding
This post makes me excited about improving my l Helpful hints
I found this very interesting. Check out moving company near me for more
срочное оформление загранпаспорта срочное оформление загранпаспорта .
discount ed pills: FastPillsEasy – ed online pharmacy
cheap ed meds online FastPillsEasy where can i get ed pills
Можно ли купить аттестат о среднем образовании? Основные рекомендации
I am glad we used them as our construction company, they were able to get the job done as well as well as stay on budget. Professional outfit, the metal studs and sheetrock came out great for the office area Clifton
Aw, this was an incredibly good post. Taking the time and actual effort to produce a superb article… but what can I say… I procrastinate a whole lot and don’t seem to get anything
done.
Как правильно приобрести диплом колледжа или ПТУ в России, важные моменты
sildenafil online: FastPillsForMen.com – over the counter sildenafil
Generic Tadalafil 20mg price buy cialis online Cheap Cialis
Сколько стоит диплом высшего и среднего образования и как его получить?
сколько стоит сделать загранпаспорт http://www.cinemania.forum24.ru/?1-15-0-00001690-000-0-0-1735288325 .
Urban Nightlife 부산유흥
Medicine information leaflet. Effects of Drug Abuse.
brand name synthroid price
Best what you want to know about meds. Read here.
카지노사이트 카지노사이트 카지노사이트 카지노사이트
카지노사이트 카지노사이트 카지노사이트
Hi there friends, how is all, and what you desire to say about this post, in my view its in fact
remarkable for me.
There are various tools and websites that allegation to allow users to view private Instagram profiles, but it’s important to log on these subsequent to caution. Many of these tools can be
unreliable, may require personal information, or could violate Instagram’s terms of
service. Additionally, using such tools can compromise your own security or guide to scams.
The safest and most ethical habit to view a free private instagram viewer app profile is to send a follow request directly to the
user. Always prioritize privacy and admiration in your online interactions.
Пошаговая инструкция по официальной покупке диплома о высшем образовании
Well explained. Discover more at pressure cleaning
Viagra generic over the counter: FastPillsForMen.com – Viagra generic over the counter
Alt coin обменник – Альткоин, Альт коин обменник
https://www.vizitnews.ru — Посетите официальный сайт vizitnews для подробной информации.
cheapest ed treatment: erectile dysfunction online prescription – cheap ed treatment
카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트
Immigrants from Afghanistan, they first arrived in America within the 1970s.
How Poker And Personal Finance Relate 에볼루션 마틴
accounting firm
Hi there just wanted to give you a brief heads
up and let you know a few of the pictures aren’t loading correctly.
I’m not sure why but I think its a linking issue.
I’ve tried it in two different internet browsers and both show the same results.