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
Hmm it looks like your blog ate my first comment (it
was super long) so I guess I’ll just sum it up what I submitted and say, I’m thoroughly enjoying
your blog. I too am an aspiring blog blogger but I’m still new to the whole thing.
Do you have any suggestions for rookie blog writers?
I’d genuinely appreciate it.
Here is my blog post Daycare Near Me
can i purchase cheap celebrex without prescription
https://cytpremium.com/# buy cytotec
Cocktail Lounge 유흥
https://cytpremium.com/# buy cytotec pills online cheap
http://lisinopril1st.com/# Lisinopril 1st
Hi would you mind letting me know which hosting company you’re working with?
I’ve loaded your blog in 3 completely different
web browsers and I must say this blog loads a lot quicker
then most. Can you suggest a good web hosting provider at a honest price?
Many thanks, I appreciate it!
Hi are using WordPress for your site platform? I’m new to the
blog world but I’m trying to get started and
create my own. Do you require any html coding expertise to make your own blog?
Any help would be greatly appreciated!
With havin so much content do you ever run into any problems
of plagorism or copyright infringement? My site has a lot of
exclusive content I’ve either authored myself or outsourced but
it looks like a lot of it is popping it up all over the web without my
authorization. Do you know any solutions to help protect against content from being stolen? I’d truly appreciate it.
Wine Tasting 광주알밤
http://lisinopril1st.com/# Lisinopril 1st
Сколько стоит диплом высшего и среднего образования и как это происходит?
10 Meetups On Electric Stove Fireplace You Should Attend electric fireplace stove
Как правильно купить диплом колледжа и пту в России, подводные камни
Как купить диплом о высшем образовании с минимальными рисками
buy cytotec pills: buy cytotec online – buy cytotec pills online cheap
Официальная покупка диплома ПТУ с упрощенной программой обучения
Приобретение школьного аттестата с официальным упрощенным обучением в Москве
Howdy! This is my first visit to your blog! We are a
collection of volunteers and starting a new initiative in a community in the same
niche. Your blog provided us beneficial information to work
on. You have done a extraordinary job!
Возможно ли купить диплом стоматолога, и как это происходит
best darknet markets darknet websites https://mydarknetmarketlinks.com/
buy priligy max pharm: max pharm – priligy maxpharm
This guide has me considering an upgrade to my office with some sleek laminate flooring—thanks! Hill Country Flooring & Construction
cytotec buy online usa: cyt premium – buy cytotec
buy cytotec online buy cytotec online Cytotec 200mcg price
вывод из запоя с выездом краснодар вывод из запоя с выездом краснодар .
can i order clomid for sale: can i buy clomid without prescription – order clomid pills
Hey There. I found your blog using msn. This is an extremely well written article.
I’ll make sure to bookmark it and come back
to read more of your useful information. Thanks for the
post. I will definitely comeback.
фонтбет фонтбет .