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
You have made some really good points there. I looked on the
internet for more information about the issue and found most
individuals will go along with your views on this website.
This is a really good tip particularly to those new
to the blogosphere. Brief but very precise information… Appreciate your sharing this one.
A must read article!
XAP Дизайн человека https://design-human.ru Дизайн человека. 3/6 Дизайн человека.
levitra discount pharmacy: hometown pharmacy – sam’s club pharmacy hours
I am sure this piece of writing has touched all the internet users,
its really really good piece of writing on building up new weblog.
How To Build A Successful Best Mobility Scooters When You’re Not Business-Savvy Reliable Mobility Scooters (https://Hobam1.Com/)
Hi there it’s me, I am also visiting this site regularly, this web site is in fact fastidious
and the visitors are truly sharing nice thoughts.
You said it adequately.!
https://usagamblinghub.com/wild-review/
Реально ли приобрести диплом стоматолога? Основные этапы
generic viagra pharmacy review: methotrexate pharmacy mistake – mexico online pharmacy
Stay secure and private online with vpn download for pc advanced protection. Whether you’re using public Wi-Fi or accessing geo-blocked content.
At this time it seems like Drupal is the
top blogging platform out there right now.
(from what I’ve read) Is that what you are using on your blog?
global rx pharmacy irvine: can you use target pharmacy rewards online – online pharmacy tretinoin
Where Can You Get The Best Private ADHD Diagnosis Information? where to Get diagnosed with add
Профессиональный сервисный центр по ремонту фото техники от зеркальных до цифровых фотоаппаратов.
Мы предлагаем: срочный ремонт фотоаппаратов
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
I ԝill гight aѡay snatch үour rss as I can nott tߋ find your email subscription link oг
e-newsletter service. Ꭰo you’ve ɑny? Plеase ⅼet me recognise sso tһat I maу jսst subscribe.
Тhanks.
Here iss my web pɑge; discuss
Heya i am for the first time here. I came across this board
and I find It truly helpful & it helped me out much. I’m hoping to give one thing back and help others like you helped me.
Feel free to surf to my webpage … 무료스포츠중계
PYD Дизайн человека https://designmsu.ru Дизайн человека. 4/6 Дизайн человека.
Ventolin inhalator: mexican online pharmacies – priligy online pharmacy
15 Private ADHD Assessment Edinburgh Benefits Everyone Must Know private adhd assessment ipswich
thriftway pharmacy store locator: superdrug pharmacy doxycycline – loratadine pharmacy choice
What’s Happening i’m new to this, I stumbled upon this I
have found It absolutely helpful and it has aided me out loads.
I am hoping to contribute & aid different users like its helped me.
Good job.
Thanks for the auspicious writeup. It in reality used to be a leisure account it.
Look complex to more brought agreeable from you! However, how could we keep in touch?
Hello! Someone in my Facebook group shared this website with us so I came
to look it over. I’m definitely loving the information.
I’m book-marking and will be tweeting this to my followers!
Exceptional blog and amazing style and design.
You made some really good points there. I checked on the net for more information about the issue and found most individuals will go along with your views on this website.
Why Adding A Car Key Programing To Your Life Will Make All The Different key programmers
Why We Enjoy Renault Clio Replacement Key Cost (And You Should Also!) replacement car keys renault – Emplois.Fhpmco.fr,
It’s not my first time to pay a visit this web page, i am browsing this web site
dailly and take pleasant facts from here all the time.
When someone writes an post he/she keeps the image of
a user in his/her brain that how a user can know it.
Thus that’s why this piece of writing is outstdanding. Thanks!
Hey there just wanted to give you a quick heads up and let you know a few of the pictures aren’t loading correctly.
I’m not sure why but I think its a linking issue. I’ve tried it in two different browsers and both show the same results.