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
вывод из запоя в стационаре вывод из запоя в стационаре .
20 Resources That Will Make You More Successful At Buy Taxi Driving License Online Without Exam
internationellt köRkort – Transportstyrelsen
выведение из запоя в нижнем новгороде выведение из запоя в нижнем новгороде .
вывод из запоя в стационаре вывод из запоя в стационаре .
advice https://jaxx-liberty.com/
I like what you guys are up too. Such clever work and exposure!
Keep up the wonderful works guys I’ve incorporated you
guys to my own blogroll.
deep web search deep web links https://github.com/darknetmarkets24/darknet-markets – darknet drug store
вывод из запоя в нижнем новгороде вывод из запоя в нижнем новгороде .
Unbelievably user friendly site. Great info available on few gos to.
https://www.kopalniapracy.pl
вывод из запоя в стационаре воронежа вывод из запоя в стационаре воронежа .
An additional crucial facet of efficiency training in esports is the psychological side of the game.
See What How To Get An ADHD Diagnosis UK Tricks The Celebs Are
Using how to get an adhd diagnosis uk
выведение из запоя воронеж стационар выведение из запоя воронеж стационар .
I am not sure where you are getting your information, but good topic.
I needs to spend some time learning more or understanding
more. Thanks for wonderful information I was looking for this information for my mission.
The material is very exciting.
nizanskie.info
A fascinating discussion is worth comment. I think that you
ought to publish more about this issue, it may not be a taboo
matter but generally people don’t speak about these topics.
To the next! Cheers!!
Also visit my web page – thankyou.Eoapps.Co.kr
These are actually wonderful ideas in about blogging.
You have touched some fastidious things here. Any way keep up wrinting.
You should be a part of a contest for one of the best blogs on the web.
I most certainly will recommend this blog!
Hello to all, how is everything, I think every one is getting more from this web page, and your views are good
in support of new visitors.
I blog frequently and I seriously appreciate your content.
This great article has truly peaked my interest. I am
going to bookmark your blog and keep checking for new details about once per
week. I subscribed to your Feed too.
вывод из запоя в стационаре вывод из запоя в стационаре .
купить диплом в иркутске
вывод из запоя в стационаре нижнего новгорода vyvod-iz-zapoya-v-stacionare21.ru .
I’m not sure exactly why but this site is loading incredibly slow for
me. Is anyone else having this problem or is it a
issue on my end? I’ll check back later on and see if the problem
still exists.
I don’t even know how I ended up here, but I thought this
post was great. I do not know who you are but certainly you are
going to a famous blogger if you aren’t already 😉 Cheers!
Hi there, just became alert to your blog through Google, and
found that it is truly informative. I am going to watch out for
brussels. I’ll appreciate if you continue this in future.
A lot of people will be benefited from your writing.
Cheers!
darknet marketplace dark web access https://github.com/darknetmarkets24/darknet-markets – darknet markets
вывод из запоя стационарно вывод из запоя стационарно .
Caluanie Muelear Oxidize For Sale buy caluanie muelear oxidize canada
See What Buy UK Driving License Tricks The Celebs Are Utilizing buy uk Driving license (http://www.1Moli.top)