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
Thanks , I’ve just been looking for info about this topic
for a long time and yours is the greatest I have found out so far.
However, what in regards to the conclusion? Are you certain about the source?
Why You Should Focus On Improving French Fridge Freezer Uk best French door fridge with water dispenser
Why You Should Concentrate On Making Improvements To Remote Control
Anal Butt Plug anal Plugs
20 Tools That Will Make You More Efficient At What Is French Door Fridge built in french door fridge freezer
(Dakota)
reputable indian pharmacies Indian pharmacy online india pharmacy mail order
top 10 online pharmacy in india Indian pharmacy international shipping cheapest online pharmacy india
Wow, this article is fastidious, my younger sister is analyzing such
things, so I am going to tell her.
cheapest online pharmacy india online Indian pharmacy best india pharmacy
Incredible points. Solid arguments. Keep up the great spirit.
Stop by my homepage เครดิตฟรี58บาท
Профессиональный сервисный центр по ремонту посудомоечных машин с выездом на дом в Москве.
Мы предлагаем: ремонт пмм
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
india pharmacy mail order: online Indian pharmacy – reputable indian pharmacies
Thank you a lot for sharing this with all people
you really recognise what you’re talking approximately!
Bookmarked. Please also discuss with my site =). We may
have a link exchange arrangement among us
mexican drugstore online mexican pharmacy mexican drugstore online
mexico pharmacies prescription drugs mexican rx online buying prescription drugs in mexico online
cheapest online pharmacy india: online Indian pharmacy – indian pharmacy
“After reading your article I was amazed. I know that you explain it very well. And I hope that other readers will also experience how I feel after reading your article.
” 한국야동
Всё, что нужно знать о покупке аттестата о среднем образовании
worldgonews.ru/nastoyashhie-diplomyi-na-zakaz-garantiruem-kachestvo
canadian drug: canada pharmacy reviews – reliable canadian pharmacy
25 Surprising Facts About Sports Toto Near Me Today 스포츠토토 – Broderick,
https://mexicanpharma.icu/# mexico pharmacies prescription drugs
mexican pharmaceuticals online: medication from mexico – buying from online mexican pharmacy
Огромный выбор слотов, рулеток и других игр казино. Бонусы на каждый депозит, фриспины и многое другое. Ставки на спорт онлайн с высокими коэффициентами most bet
모든 유흥의 성지 강남!! 그 중에서도 여성분들에게 단연 인기 많은 곳은? 바로 저희 강남호빠입니다.
https://indiadrugs.pro/# best india pharmacy
mexican rx online mexican pharmacy mexican mail order pharmacies
bokep terbaik sma toket gede menyala banget
Aw, this was an exceptionally good post. Spending some time and actual effort to
produce a great article… but what can I
say… I put things off a whole lot and don’t manage to get anything done.
I’m not sure exactly why but this site is loading incredibly slow for me.
Is anyone else having this issue or is it a issue on my end?
I’ll check back later on and see if the problem still exists.
I have read so many content concerning the blogger lovers but this
article is truly a pleasant post, keep it up.
I loved as much as you will receive carried out right here.
The sketch is tasteful, your authored material stylish.
nonetheless, you command get got an shakiness over
that you wish be delivering the following.
unwell unquestionably come more formerly again as exactly the same nearly a lot often inside
case you shield this hike.
Here is my website – USA Script Helpers