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
Great tips! For more, visit best live casinos
This was quite informative. For more, visit Comment choisir un casino
Hi there, after reading this amazing post i am also cheerful to share my experience here with mates.
I used to be able to find good advice from your blog articles.
секретный код изи дроп секретный код изи дроп .
A Look At The Myths And Facts Behind Buy A Driving License kup prawo jazdy b online [Justin]
Great blog right here! Also your web site lots up fast!
What web host are you the usage of? Can I get
your associate hyperlink for your host? I desire my web site loaded up as fast
as yours lol
купоны изи дроп купоны изи дроп .
I really like your blog.. very nice colors & theme.
Did you create this website yourself or did you hire
someone to do it for you? Plz respond as I’m looking to construct my own blog and would like
to know where u got this from. cheers
What’s Happening i’m new to this, I stumbled upon this I have discovered It positively helpful and it has
helped me out loads. I hope to give a contribution & aid different customers like its aided me.
Great job.
Tax Return Online – Makes Life Easy 다바오충전
This was highly educational. For more, visit drain cleaning,
11 Strategies To Completely Redesign Your Replace
Upvc Window Handle Door Handle Repair Near Me
Does your site have a contact page? I’m having a tough time
locating it but, I’d like to send you an e-mail.
I’ve got some recommendations for your blog you might be interested in hearing.
Either way, great blog and I look forward to seeing it develop over time.
A Time-Travelling Journey: How People Talked About Fabric 2 Seater 20 Years Ago Fabric Sofa 2 Seater
промокоды easydrop промокоды easydrop .
https://casinositeleri25.com/# guvenilir casino siteleri
промокоды на easydrop промокоды на easydrop .
slot casino siteleri: az parayla cok kazandiran slot oyunlar? – az parayla cok kazandiran slot oyunlar?
вывод из запоя екатеринбург вывод из запоя екатеринбург .
вывести из запоя цена вывести из запоя цена .
I know this web site gives quality depending posts
and additional material, is there any other site which offers
such data in quality?
Hi I am so happy I found your web site, I really found you by error, while I was browsing on Google for something else, Anyways I am here now
and would just like to say thanks a lot for a
incredible post and a all round enjoyable blog (I also love the theme/design),
I don’t have time to read it all at the moment but
I have bookmarked it and also added in your RSS feeds, so when I have time I will
be back to read more, Please do keep up the great work.
Hi mates, good paragraph and nice arguments commented here, I am actually enjoying by these.
промо изи дроп промо изи дроп .
https://mostbet-wre3.xyz/ о возможностях и преимуществах ставок на спорт в Mostbet – быстрые выплаты, широкая линия событий и привлекательные бонусы для новых пользователей.
Greetings I am so glad I found your blog, I really found
you by error, while I was researching on Google for something
else, Regardless I am here now and would just like
to say cheers for a remarkable post and a all round interesting blog (I
also love the theme/design), I don’t have time to go through it
all at the moment 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 great job.
Hello, everything is going fine here and ofcourse every one is sharing information, that’s truly good, keep up writing.
https://sweetbonanza25.com/# sweet bonanza demo oyna
https://denemebonusuverensiteler25.com/# yat?r?ms?z deneme bonusu veren siteler