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
Walthamstow door panels
Thanks for another fantastic article. Where else may anyone get that type of information in such
an ideal means of writing? I have a presentation next week, and I am
at the search for such information.
Door Specialists Walthamstow
If you’re looking for a reliable way to buy and sell online, a verified Cash App account is the answer.
With verified status, you gain access to enhanced security features
and the ability to send and receive higher transaction limits.
Don’t miss out on the convenience and peace of mind that a verified
Cash App account offers. Get yours today!
%%
Feel free to visit my site :: cs battle case (https://www.regs.rw/)
Hey there! I know this is somewhat off-topic however I
needed to ask. Does running a well-established blog such as yours take a massive amount work?
I’m completely new to operating a blog however I do write in my diary every day.
I’d like to start a blog so I can easily share my own experience and thoughts online.
Please let me know if you have any kind of ideas or tips for brand new aspiring blog
owners. Appreciate it!
double glazing in corby, Josefina,
Здесь можно не дорого и качественно оформить Справка 095 у
Fatаstіc beat ! I wish to apρrentice while you amend your site, hoᴡ caan i subscribe for a blog ᴡeb site?
The accοunt helped me a ɑcceptable deal. I һad been a little bit acquainted of this your broadcaѕt proνidsed bright clеar concept
Vamos ler este guia definitivo de como fazer campanhas do TikTok e aprender com os 10 melhores exemplos do TikTok.
A Comprehensive Guide To Washing Machines Built In. Ultimate Guide To Washing
Machines Built In
My web blog: efficient
You have very nice post and pictures, please have a look at
our photo tours in the temples of Angkor
gob.ar
You said this terrifically.
Porn videos
%%
my blog post … flexible work from home Jobs
sash window repair kingston (https://haas-zachariassen-2.technetbloggers.de)
dallas asbestos lawyer (Asbestoscompensationlawye30227.link4blogs.com)
%%
Here is my blog post … buy full spectrum cbd
oil [Ralph]
upvc windows berkhamsted (Lyle)
I am regular reader, how are you everybody? This piece
of writing posted at this web site is genuinely good.
Hi friends, how is the whole thing, and what you wish for to say about this paragraph,
in my view its genuinely remarkable in support of me.
door repairs Corby
%%
Feel free to surf to my web page – double glazed windows hackney (Yolanda)
designer handbags Popular
%%
Also visit my web site … reps uk (m.En.thesuperplay.com)
Don’t Make This Mistake With Your Customize Your custom wedding dresses (Gustavo) Dress
%%
my page :: size 7 for shoes (Maximo)
%%
Feel free to surf to my homepage :: real life silicone love dolls
(Brigida)
Asbestos claims facility
Nicely put. With thanks!