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
My brother recommended I may like this website. He used to
be totally right. This post actually made my day. You can not imagine
simply how much time I had spent for this
information! Thanks!
%%
My page; Car key cover volkswagen (http://www.koreafurniture.com)
You actually stated that adequately.
Here is my webpage https://boy-fuck.com
Thanks extremely useful. Will certainly share website with my friends.
https://www.polskibiznes.info
sex
%%
My web-site: fresh espresso beans; Francesca,
merci beaucoup !
What’s up to all, the contents existing at this site are truly awesome for people
knowledge, well, keep up the nice work fellows.
%%
Review my homepage; mobility scooters to buy
Great goods from you, man. I have have in mind your stuff previous to and
you are simply too excellent. I really like what you’ve bought right here,
certainly like what you’re stating and the way in which you
assert it. You make it enjoyable and you still take care of to stay it wise.
I can not wait to learn much more from you. This is really a terrific site.
%%
Feel free to visit my blog Replacement Saab Key (https://Www.Dongjin-E.Com/Bbs/Board.Php?Bo_Table=Free&Wr_Id=4665)
I feel this is among the most important information for
me. And i am glad reading your article. But want to remark on few general
things, The site style is great, the articles is actually great :
D. Excellent task, cheers
replacement double glazing windows (https://chessdatabase.science/)
salem mesothelioma Attorney
%%
My site Skoda Key Replacement – Leewhan.Com,
%%
Here is my homepage window handles replacement
%%
Have a look at my web site; cheapest full spectrum
cbd oil uk (Xavier)
Window repairs near me
%%
Here is my page truck attorneys near Me
%%
my blog post :: Childrens Bunk Bed
Everyone loves what you guys tend to be up too. This sort of clever work and coverage!
Keep up the very good works guys I’ve included you guys to blogroll.
Remote Controlled buttplug
%%
My site; Kia Keys Replacement
Mesothelioma Lawsuits Settlements
Good way of explaining, and pleasant post to get facts concerning my presentation topic, which i am going to convey in academy.
%%
My blog :: saab key replacement near me
You should take part in a contest for one of the finest blogs on the web.
I most certainly will highly recommend this site!
mesothelioma entitled to financial compensation
Upvc Window Repairs Macclesfield (Te.Legra.Ph)
%%
my web page; cs case opening (Erwin)