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
Authentic HGH Rewards (NewULife Hgh Gel Not Evaluated Yet).
Visit my site – Bookmarks
Genuine HGH Advantages (NewULife Hgh Gel Not Examined Yet).
Here is my page: mobile home replacement windows florida
NewULife Ranks. Invite to my NewUlife Review.
Also visit my website :: replacement windows in amarillo texas
In this situation, the life insurance policy firm’s money.
Also visit my web page … orthopedic surgeon specialist
Some individuals vape flavored nicotine
my web page – plant based alternative To vaping
They have items to sell or services to offer.
My website – volkswagen service center edison nj (Christy)
They have products to offer or services to provide.
Feel free to surf to my web site home theater system with dvd player
HQJ Дизайн человека https://humandesignplanet.ru Дизайн человека. 3/5 Дизайн человека.
Our competence reaches massive business projects.
My webpage – Bookmarks
Nine Things That Your Parent Teach You About Daftar Akun Togel
Resmi daftar akun togel resmi – Bonny,
This vaporizer has a couple of special features.
Feel free to surf to my webpage :: where to buy plant based meals
Make use of the most detailed legal brochure of kinds.
My web blog; Bookmarks
NewULife Ranks. Welcome to my NewUlife Review.
Also visit my blog post; home theater speakers bluetooth –
Jami –
Компании на voskrcerkov.ru
USP human development hormonal agent (somatropin).
Also visit my website … Bookmarks
10 Double Glazed Window Hinge Tricks All Pros Recommend
how much does it cost to replace window Hinges
online pharmacy tegretol xr: finasteride target pharmacy – rx outreach pharmacy
Ideal Arizona Wrongdoer Protection Attorney.
Feel free to visit my web-site – bankruptcy lawyers springfield mo
What is New U Life SOMADERM Transdermal Gel.
Also visit my blog post – Bookmarks
Hi there to every , since I am genuinely
eager of reading this blog’s post to be updated daily.
It carries fastidious information.
A synthetic USP human development hormone (somatropin).
Take a look at my web-site; cheap farm wedding venues texas
The 10 Scariest Things About U Shaped Sectional Leather u shaped sectional leather, Rueben,
Unlike other professionals, we value your time.
My blog post: somaderm gel on amazon
Lawful Aid offices provide help Bankruptcy lawyers in oakland california a selection of areas.
You’ll Never Be Able To Figure Out This Situstoto
Slot’s Tricks situstoto slot (Ines)
Buzzwords De-Buzzed: 10 Other Methods To Say Smart Car Key Replacement Near Me smart key replacement near me; Phillipp,
NewULife Ranks. Invite what to wear to a texas ranch wedding my NewUlife Evaluation.
online pharmacy viagra reviews: ketoconazole pharmacy – tesco pharmacy lariam
Hi i am kavin, its my first time to commenting anyplace, when i read
this article i thought i could also make comment due to this good article.
Here is my homepage; memek
Если вы искали где отремонтировать сломаную технику, обратите внимание – выездной ремонт бытовой техники в челябинске