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
mesothelioma lawsuit attorneys (competent-rabbit-g0qp03.mystrikingly.com)
%%
my blog post … join avon [http://www.ordinarypeople.co.kr/]
poisoning
%%
Here is my page; how long does it take to Get a Mesothelioma settlement
%%
My page – cbd oil uk shop (Gilda)
%%
Here is my webpage :: auto Key locksmith
That is a great tip particularly to those fresh to the blogosphere.
Brief but very accurate information… Many thanks for sharing this one.
A must read post!
%%
Here is my web page :: Auto ghost immobiliser
windows repairs (Colette)
10 Wrong Answers For Common railroad cancer lawsuit settlements Lawsuit Chronic Obstructive Pulmonary Disease
Questions Do You Know The Right Ones?
%%
my blog post :: repairer
Hey! I know this is kind of off topic but I
was wondering which blog platform are you using for this
website? I’m getting tired of WordPress because I’ve had problems with hackers
and I’m looking at alternatives for another platform.
I would be great if you could point me in the direction of a
good platform.
%%
Also visit my web site … organic cbd oil vape (Callum)
%%
Look into my page – cbd bath bombs in my area (shinhwapack.co.kr)
Heya i’m for the first time here. I found this board and I find It truly helpful & it helped me out a lot.
I hope to present something again and aid others such as you
helped me.
%%
Feel free to surf to my homepage – where to buy Broad spectrum Cbd
пчелка майя медовый движ ссылка
%%
My web-site: average Mesothelioma settlement Amount
ghost Installations
Asbestos Claims In South Africa
%%
Feel free to surf to my web-site – Asbestos Litigation [Luxuriousrentz.Com]
%%
Take a look at my web site home from Work Jobs
Thank you, I’ve just been searching for info approximately this topic for
a long time and yours is the best I have discovered so far.
However, what about the bottom line? Are you positive about the source?
media & tech http://accentmedia.eu
Good day! Do you use Twitter? I’d like to follow you if that would be ok.
I’m definitely enjoying your blog and look forward to new posts.
Hi, I think your blog might be having browser compatibility issues.
Whenn I take a look at your sikte in Safari, it looks fine however, if opening in I.E.,
it has some overlapping issues. I simply wanted too give you a quick heads up!
Aside from that, wonderful blog!
my blog post: xxxx pornos gratis
осман гази сериал на русском языке
Repair double glazing windows
amersham double glazing (click4r.com)
%%
Feel free to visit my web site – toyota aygo key replacement (http://vk1bi647en0fvwfy1byxw9pm.com/)
I rеally like whatt yоu guys tend to be upp too. This kind
of clever work and exposure! Keep up the ɑmazing works
guys I’ve includeԁ you guhys to my own Ƅlogroll.