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
best price for viagra 100mg: FastPillsForMen – viagra canada
What is New U Life SOMADERM Transdermal Gel.
Also visit my site; Hvac Seattle
Allow me show you in my review of NewULife.
Review my web page – hvac service seattle wa
An artificial USP human growth hormone (somatropin).
My website – Hvac contractors Seattle
What is New U Life SOMADERM Transdermal Gel.
My web-site: hvac seattle
great issues altogether, you simply won a logo new reader.
What could you suggest in regards to your publish that you just made some days in the past?
Any positive?
купить диплом в хабаровске
In addition to understanding the principles of their own HOA, homeowners in Jennings, Louisiana also needs to be aware of state legal guidelines governing insurance protection in order to ensure they’re adequately coated.
Why You’re Failing At Asbestosis Asbestos Mesothelioma Attorney Mesothelioma Lawsuit
This blog post is super helpful! Roof replacement seems less intimidating now. For expert advice, don’t forget to visit new roof installation
This was quite informative. For more, visit copiers near albany, ny
Telegram 係一款流行嘅即時通訊應用,紙飛機官網提供中文版本,用戶可以点击下載 紙飛機 。呢個平台唔單止有電腦桌面版,仲提供適用於蘋果手機同安卓設備嘅 APK 版本。
Bangkok Guest Friendly Hotels – Where To Shop And Stay 부산
cheap erection pills: best online ed pills – cheapest ed pills
Appreciate the thorough write-up. Find more at Klicken Sie hier für die Quelle
Spot on with this write-up, I truly think this web site needs much more attention. I’ll
probably be back again to read through more, thanks for the info!
My spouse and I stumbled over here different page and thought I should check things out.
I like what I see so i am just following you.
Look forward to looking at your web page for a second time.
Flowers can evoke such strong memories and feelings, don’t you assume? They have a means of attaching us to minutes in time. Share your ideas with me at buy roses online
This was nicely structured. Discover more at برگرداندن پست حذف شده اینستاگرام
Wonderful beat ! I wish to apprentice even as you amend your website, how can i subscribe for a blog web site?
The account helped me a acceptable deal. I have been tiny bit
acquainted of this your broadcast offered vivid transparent idea
cheapest viagra: Fast Pills For Men – buy viagra here
срочное оформление загранпаспорта срочное оформление загранпаспорта .
A simple change like adding screens can make your outdoor area feel so much cozier—head over to Pool screen repair for
https://fastpillsformen.com/# Cheap Sildenafil 100mg
купить диплом о высшем образовании в москве
срочно сделать загранпаспорт срочно сделать загранпаспорт .
Tis The Time Of Year To Prosper: Smart Holiday Marketing To Ramp Up Your Revenues 알밤
Nothing beats a sundown cruise in Austin! I located an distinctive boat apartment provider at banana boat
Nicely put, Thank you!
10 Things We All We Hate About Boarding Up Door
Boarding Up Windows And Doors (Fkwiki.Win)