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
Mobile vaporizers make use of batteries as a power source.
Visit my web page: Bookmarks
Le промокод на бесплатную ставку 1хбет на сегодня, vous offre la possibilite de beneficier d’un bonus de bienvenue de 200% sur votre premier depot en Afrique, au lieu du taux habituel de 100% (voire meme de 130% au lieu de 200% dans certains pays). Vous pouvez obtenir jusqu’a 130€ ou l’equivalent dans votre devise.
USP human development hormone (somatropin).
Here is my web site – replacement windows austin texas
They have products to offer or solutions to offer.
Here is my site :: home theater seating layout
Firmly make payment to employ your legal representative.
My web page … list of chapter 11 bankruptcies
This vaporizer has a couple of distinct functions.
Here is my web page :: plant based vape australia
What is New U Life SOMADERM Transdermal Gel.
Feel free to visit my website: replacement windows texas
kroger pharmacy crestor: online international pharmacy – generic viagra us pharmacy
The bigger Slim battery, fantastic for longer runtimes.
my page … foil drive assist max
Our knowledge extends to large commercial projects.
Look at my web site: new u life somaderm gel side effects
Real HGH Benefits (NewULife Hgh Gel Not Tested Yet).
Feel free to visit my website volkswagen service center nj
A synthetic USP human development hormonal agent
(somatropin).
My homepage: best ceramic pottery wheels
5 Killer Quora Answers To Daftar Situs Togel daftar Situs togel
They have products to offer or services to provide.
Here is my webpage: replacement windows brevard county florida
anastrozole pharmacy: weight loss – metoprolol succinate online pharmacy
What is New U Life SOMADERM Transdermal Gel.
My website: shoulder surgeons colorado springs
The bigger Slim battery, wonderful for longer runtimes.
Look into my web-site Bookmarks
Our competence includes massive business jobs.
my site when did new u life start
What is New U Life SOMADERM Transdermal Gel.
Feel free to surf to my web-site; foreign auto repair
This vaporizer has a couple of one-of-a-kind functions.
my blog; plant based vape australia
Все бонусы и фрибеты доступны на https://888starz.today, присоединяйтесь к игре прямо сейчас.
In this situation, the life insurance policy company’s loan.
My site :: Ceramic Pottery wheel Machine
Awesome site you have here but I was curious about if you knew of any message boards that cover the same topics discussed here?
I’d really like to be a part of group where I can get opinions from other knowledgeable individuals that share the same interest.
If you have any suggestions, please let me know.
Many thanks!
There are several sources of Infantile Spasms Signs That A Child Is Having A Seizure spasms.
Authentic HGH Perks (NewULife Hgh Gel Not Checked Yet).
Also visit my homepage; ceramic Pottery wheel
The vapor and flavor quality is also wonderful.
my webpage; Bookmarks
They have items to market or services to offer.
Here is my web-site: orthopedic surgeons near me now
NewULife Ranks. Invite to my NewUlife Evaluation.
Also visit my homepage :: shoulder surgeon specialist near me
I am in fact grateful to the holder of this site who has shared this wonderful article at at this
time.
They have items to market or services to provide.
Also visit my webpage texas rustic wedding Venues