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
I’m really impressed with your writing skills and also with the layout on your blog.
Is this a paid theme or did you modify it yourself?
Anyway keep up the nice quality writing, it is rare to
see a great blog like this one these days.
http://lisinopril1st.com/# lisinopril1st
Gentlemen’s Club 하이오피
Процесс получения диплома стоматолога: реально ли это сделать быстро?
http://iverfast.com/# minocycline 50 mg para que sirve
Узнайте, как приобрести диплом о высшем образовании без рисков
Abortion pills online cyt premium buy cytotec over the counter
http://cytpremium.com/# buy cytotec online
Легальные способы покупки диплома о среднем полном образовании
Приобретение школьного аттестата с официальным упрощенным обучением в Москве
A Peek Inside The Secrets Of Fleshlight Fleshlight Brand
Club Vibes 출장안마
Peculiar article, exactly what I was looking for.
Официальная покупка диплома вуза с сокращенной программой в Москве
The tests also revealed that most people https://safestcasinosites.co.uk/.
Good day I am so delighted I found your website, I really
found you by mistake, while I was searching on Google for something else, Anyhow
I am here now and would just like to say cheers for
a tremendous post and a all round entertaining blog (I
also love the theme/design), I don’t have time to look over it all at
the minute but I have book-marked it and also included your RSS feeds, so when I have time I will be back to read a lot more,
Please do keep up the superb jo.
Полезные советы по безопасной покупке диплома о высшем образовании
Быстрая схема покупки диплома старого образца: что важно знать?
Как получить диплом техникума с упрощенным обучением в Москве официально
Club Vibes 잠실오피
darkmarket dark market onion dark market
amoxicillin 500mg capsules antibiotic: amoxicillin order online no prescription – amoxicillin 500mg capsules uk
Hey very interesting blog!
hello!,I love your writing very so much! share we be in contact more about your article on AOL?
I need an expert on this area to resolve my problem.
May be that is you! Having a look forward to peer
you.
I blog quite often and I really thank you for your information. Your article has
really peaked my interest. I am going to bookmark
your website and keep checking for new details about once per week.
I subscribed to your Feed too.
Social Club 출장 (http://www.3dgamemarket.Net)
Poker For Beginners Davao
Купить диплом о среднем образовании в Москве и любом другом городе
Most activity trackers start out with a fixed goal – usually around
10,000 steps – or let you set your individual manually,
and there it ends.
Also visit my webpage สล็อตออนไลน์
Wow! At last I got a blog from where I know how to genuinely
get valuable facts regarding my study and knowledge.