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
What’s The Job Market For Can Mobility Scooters Go On Road Professionals?
can mobility scooters go on road (Rodrick)
I’m really inspired together with your writing talents as neatly as with the structure in your
blog. Is this a paid theme or did you customize it yourself?
Either way stay up the excellent quality writing, it’s uncommon to see a great
blog like this one these days..
BBgate MarketPlace 2024 Breaking Bad Gate Forum
BBgate MarketPlace
I every time spent my half an hour to read this webpage’s content daily along with
a cup of coffee.
order generic clomid online: clomid – where to get cheap clomid without dr prescription
What’s up, just wanted to say, I loved this post.
It was inspiring. Keep on posting!
It’s very straightforward to find out any topic on net as compared to textbooks, as I found this
article at this web page.
max pharm: priligy maxpharm – cheap priligy
Medication prescribing information. Short-Term Effects.
where can i buy cheap azathioprine without a prescription
All news about meds. Get information now.
medicine amoxicillin 500mg: Amoxicillin Com Pharm – amoxicillin 500 mg where to buy
How To Know The Convertible Sleeper Sofa That’s Right
For You Comfortable Sleep Sofas
It’s The Perfect Time To Broaden Your Renault Megane Key Card Replacement Options renault kadjar keyless entry
how to buy amoxycillin: amoxil com pharm – amoxicillin capsules 250mg
cheap priligy: dapoxetine online – buy priligy
Всем привет! Подскажите, где найтиполезные блоги о недвижимости? Пока нашел – https://finkom74.ru
фабрика кухня — Прямые поставки от производителя: кухни высокого качества.
My family members all the time say that I am wasting my
time here at net, however I know I am getting experience
daily by reading such nice articles.
Guide To Top Prams: The Intermediate Guide In Top Prams top prams
It’s really a nice and helpful piece of info. I’m glad that you
shared this useful information with us. Please stay us up to date like this.
Thanks for sharing.
Приветствую. Подскажите, где почитатьполезные статьи о недвижимости? Пока нашел – https://finkom74.ru
rexall pharmacy amoxicillin 500mg: Amoxicillin buy online – 875 mg amoxicillin cost
Приветствую. Может кто знает, где почитатьполезные статьи о недвижимости? Пока нашел – https://finkom74.ru
Купить диплом магистра оказалось возможно, быстрое обучение и диплом на руки
prednisone 20mg online pharmacy: prednisone ray pharm – where can i get prednisone
buy amoxicillin online mexico: Amoxicillin for sale – amoxicillin 500mg for sale uk
BBgate MarketPlace 2024 Breaking Bad Gate Forum
BBgate MarketPlace
If some one needs expert view concerning blogging and site-building then i
recommend him/her to pay a quick visit this weblog, Keep up the fastidious job.
order amoxicillin no prescription: Amoxicillin Com Pharm – amoxicillin discount coupon
Где и как купить диплом о высшем образовании без лишних рисков
Приветствую. Может кто знает, где найтиполезные блоги о недвижимости? Сейчас читаю – https://finkom74.ru