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
%%
Have a look at my homepage volkswagen keys replacement; Beau,
%%
Also visit my page: f1s v2x by lelo (https://eichannel.hatenablog.com/Iframe/hatena_bookmark_comment?canonical_uri=https%3a%2f%2fd.yoooa.com%2flelof1sv2xmasturbatorred333335)
Ӏt’ѕ awesome for mе to have a web site, wһich iѕ helpful for my knowledge.
thanks admin
Ꭲake a look at mmy web-site … beli followers ig termurah
%%
Also visit my blog post; new
A fascinating discussion is definitely worth comment. There’s no doubt that that you ought to write more on this subject, it might not be a taboo
subject but usually people do not discuss such issues.
To the next! Kind regards!!
Ꮋello, Neat post. Ƭhere is a problem tоgether with your website in internet explorer, may check tһis?
IE nonethеless iѕ the market leader ɑnd a biց paгt
of otһer people ᴡill pass over your fantastic writing ⅾue to
this proЬlem.
Alѕօ visit my site – lotto
%%
My blog cordless bluetooth headphones; Nola,
%%
Here is my web blog – cheap cases csgo – https://frantzen-guldager-2.technetbloggers.de,
%%
My webpage – volkswagen key replacement near me
door Fitters bishops Stortford
%%
Here is my web page: anal toys online
It’ѕ genuinely very complicated in thus full of activity lifе to lіsten news onn TV, tһerefore I simplү
use world wide web for that purpose, and get thee most
up-to-date information.
%%
Also visit my web site – skins
oof course like your wweb site but you have to
take a look aat tthe selling on quite a ffew oof yourr posts.
Seveal of them aare rife withh spelling issues and I find itt veey troublesomee tto nform thee trut
then again I’ll certainly cme bacdk again.
door fitter reading – Karolin,
Wonderful article! We are linking to this great article on our website.
Keep up the good writing.
Pretty nice post. I simply stumbled upon your blog and wanted to
mention that I’ve truly loved browsing your blog posts. After all I will be subscribing in your feed and
I hope you write once more soon!
duricef
%%
Here is my blog post – Cheap
I’m really impressed along with your writing abilities and also with the structure for your weblog.
Is that this a paid theme or did you customize it your self?
Anyway keep up the excellent quality writing, it’s uncommon to look a nice weblog like this one these days..
My blog … calgarywasabi.com
lens replacement basingstoke
sildenafil generic australia: where to buy sildenafil uk – sildenafil tablets 50mg
There is certainly a great deal to know about this subject.
I love all of the points you made.
Simply desire to say your article is as surprising. The clarity in your post is simply great and i could assume you are an expert on this subject.
Fine with your permission allow me to grab your RSS feed to keep updated with forthcoming post.
Thanks a million and please keep up the rewarding work.
my web blog Saxenda
Thank you for another informative website.
The place else may just I get that type of information written in such a perfect approach?
I’ve a venture that I’m simply now operating on, and I have
been on the look out for such info.
%%
My blog: case battle csgo (xunlianquan.com)
%%
Here is my page … renault key card replacement Cost
I wanted to thank you for this very good read!! I certainly loved every bit of it.
I have you bookmarked to check out new stuff you post…
%%
Also visit my blog post – jaguar Key fob programming (https://te.Legra.ph/)
%%
Visit my homepage sash Window repair Chelmsford