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
Oh my goodness! Incredible article dude! Thanks, However I
am experiencing issues with your RSS. I don’t know
why I can’t subscribe to it. Is there anybody else having similar RSS issues?
Anyone who knows the solution will you kindly respond?
Thanx!!
They have items to market or services to use.
Also visit my web-site; ceramic pottery wheel classes
Let me reveal you in my evaluation of NewULife.
Also visit my homepage: Bookmarks
This vaporizer has a couple of distinct attributes.
Feel free to surf to my blog … Bookmarks
Unlike other specialists, we value your time.
Feel free to visit my homepage :: newulife back office
The 10 Most Terrifying Things About Couches Recliner Couches recliner
mexitil online pharmacy: boots pharmacy kamagra – roman online pharmacy
Real HGH Rewards (NewULife Hgh Gel Not Evaluated Yet).
My blog post – rustic wedding venues south texas
They have products to market or services to provide.
Feel free to visit my web site – bookmarks
There are several causes of infantile convulsions.
My web blog: Bookmarks
10 Wrong Answers For Common Multi Fuel Stoves Uk Questions Do You Know The Right
Ones? Multi-fuel fireplaces
In this situation, the life insurance firm’s cash.
Visit my blog … Bookmarks
Authentic HGH Advantages (NewULife Hgh Gel Not Tested Yet).
My website; Bookmarks
снятие ломки наркологом снятие ломки наркологом .
USP human development hormonal agent (somatropin).
My site :: Import Auto Repair Shop
Allow shoulder orthopedists near me reveal you in my evaluation of NewULife.
NewULife Ranks. Invite to my NewUlife Testimonial.
Feel free to surf to my homepage :: Bookmarks
USP human growth hormonal agent (somatropin).
My page – volkswagen service center hours
pharmacy rx one legit: rx express pharmacy panama city fl – Careprost
Unlike various other contractors, we value your time.
Have a look at my webpage – new u life reviews hgh
In this instance, the life insurance company’s cash.
my blog … Bookmarks
The 10 Most Scariest Things About Lidar Robot Navigation lidar robot navigation (https://hurley-lane-2.technetbloggers.de/how-to-explain-vacuum-lidar-to-your-grandparents)
USP human growth hormone (somatropin).
My web site … Custom vinyl windows
Chat with certified lawyers up until you’re satisfied.
Look into my homepage … Bookmarks (Charles)
Meilleur промокод на деньги в 1хбет vous permettra de recevoir leur excellent bonus de bienvenue allant jusqu’a $130 sur le sport en paris gratuits. Ce bonus de bienvenue est valable un mois. La maniere la plus simple d’obtenir un code promotionnel pour 1xbet est de visiter l’un des sites partenaires. En general, ces ressources publient regulierement des codes frais dans le cadre de leur cooperation constante avec le bookmaker.
A baby can have as several as 100 convulsions a day.
Take a look at my webpage – infantile spasms symptoms nhs
The vapor and flavor high quality is likewise terrific.
My site … where can you buy vapes in australia
lorazepam fearpharmacy: generic cialis online pharmacy – online veterinary pharmacy
Mobile vaporizers make use of batteries as a power source.
Also visit my site :: canadian plant based meat companies
USP human growth hormonal agent (somatropin).
Here is my site replacement window cost average