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
Hi there, this weekend is good in support of me, as this occasion i am reading this fantastic informative
article here at my residence.
Погрузитесь в течение юдоль скорби развлечений также увлекательных ситуаций на портале Любовь 28!
любовь
How To Beat Your Boss In Bean To Cup Coffe Machine bean to cup coffee machine home
wonderful points altogether, you just gained a new
reader. What may you suggest about your put up that you just made
a few days ago? Any positive?
https://ww1.prediksioni.sbs/
deneme bonusu veren siteler betturkey betturkey.com: deneme bonusu veren siteler mycbet.com – deneme bonusu veren siteler yerliarama.org
Hello it’s me, I am also visiting this web site daily, this web site is
really pleasant and the people are truly sharing fastidious thoughts.
Why You’ll Want To Read More About Mobility Scooter For
Seniors Adjustable mobility Scooters
10 Misconceptions Your Boss Has Regarding Seat Leon Key Fob Replacement Seat mii replacement key
slot tr online: slot siteleri – az parayla cok kazandiran slot oyunlar?
купить аттестат за 11 класс в нижнем новгороде many-diplom77.ru .
Тут можно преобрести несгораемый сейф цена огнестойкие сейфы
The Best Bunk Beds For Adults Tricks To Transform Your Life best Bunk beds
Приобретение диплома ПТУ с сокращенной программой обучения в Москве
lightsdemons.phorum.pl/viewtopic.php?p=68957#68957
The Most Convincing Proof That You Need Sports Toto
토토사이트
Тут можно преобрести оружейный шкаф цена шкаф для оружия купить
Are You Responsible For An Toto Rules Budget?
12 Best Ways To Spend Your Money 메이저사이트 바록가기
The 10 Most Scariest Things About L Shaped Leather Settee l shaped Leather Settee
купить диплом спбгу many-diplom77.ru .
Greetings from Idaho! I’m bored at work so I decided to browse
your site on my iphone during lunch break. I enjoy
the info you provide here and can’t wait to take a look when I get home.
I’m shocked at how quick your blog loaded on my mobile
.. I’m not even using WIFI, just 3G .. Anyways,
excellent blog!
viewer and downloader viewer and downloader .
Тут можно преобрести сейф пожаростойкий сейфы пожаростойкие
I visited several websites however the audio quality for audio songs present at this web site is in fact
superb.
http://w1.seringjitu.cfd/
5 Laws That Anyone Working In Audi A3 Key Battery Should Know
audi Car keys
deneme bonusu veren siteler yeni https://matadorbet.bid/# matadorbet.bid
The Most Significant Issue With Fireplace On Wall, And How You
Can Fix It White Fireplace
view and download instagram stories http://freeinstviewer.com/ .
Быстрое обучение и получение диплома магистра – возможно ли это?
ultrabet giris ultrabet ultrabet bonus
15 Reasons To Love Virtual Mystery Boxes Mystery box opening, http://www.google.mn,
Реально ли приобрести диплом стоматолога? Основные шаги