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
What is New U Life SOMADERM Transdermal Gel.
my web-site – cheap wedding venues in texas
Authentic HGH Advantages (NewULife Hgh Gel Not Checked Yet).
Feel free to surf to my webpage: cheap farm wedding venues texas
WSW Дизайн человека https://rasschitat-dizayn-cheloveka-onlayn.ru Дизайн человека. 6/3 Дизайн человека.
ziprasidone online pharmacy: levaquin online pharmacy – bactrim online pharmacy
WTN Дизайн человека https://irida-design.ru Дизайн человека. 5/1 Дизайн человека.
The vapor and taste top quality is likewise fantastic.
Here is my web blog – plant based vape juice
Way cool! Some extremely valid points! I appreciate you penning this article plus
the rest of the site is very good.
What is New U Life SOMADERM Transdermal Gel.
Here is my homepage; replacement windows near me
윗동네 강남 부럽지 않은 퀄리티를 성남 룸싸롱에서 체험 해보세요.
The bigger Slim battery, terrific for longer runtimes.
Also visit my web site: Bookmarks
This vaporizer has a couple of unique features.
Feel free to surf to my web site – plant Based vape australia
Five Killer Quora Answers On Coffee Pods Machine Coffee pods Machine
I pay a quick visit day-to-day some web sites and blogs to read articles or
reviews, however this website gives quality based posts.
Как официально купить аттестат 11 класса с упрощенным обучением в Москве
XYGENYX, a licensing business for FDA-registered items.
Check out my site: window replacement near me car
Some people vape flavored pure nicotine
Here is my site – where to buy plant based meals
Unlike other contractors, we value your time.
Take a look at my webpage; Bookmarks
Blue Steel Gummies are a popular dietary supplement that claims to
enhance performance, increase energy levels, and improve overall health.
This vaporizer has a couple of one-of-a-kind features.
My blog post … where can you buy vapes in australia
Excellent blog post. I absolutely appreciate this website.
Stick with it!
The bigger Slim battery, fantastic for longer runtimes.
Here is my homepage – dual catch can setup
An infant can have as numerous as 100 At What Age Do Infantile Spasms Start a day.
Plant-based vapes have a host of possible advantages.
Here is my webpage – Bookmarks
Профессиональный сервисный центр по ремонту бытовой техники с выездом на дом.
Мы предлагаем: ремонт крупногабаритной техники в москве
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
A lot of Chapter 13 bankruptcy lawyers near me with payment plans have to be three
to five years long.
Если вы искали где отремонтировать сломаную технику, обратите внимание – тех профи
They have items to sell or services to offer.
Visit my blog post: ceramic art classes near me
Mobile vaporizers make use of batteries as a source of power.
my web site plant based vape juice
A synthetic USP human development hormone (somatropin).
My web-site :: home theater system installation
Plant-based vapes have a host of prospective advantages.
Feel free to visit my site – Bookmarks