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
20 Resources That Will Make You Better At Double Glazing Misting Repair Double Glazed Repair
ca board of pharmacy: smith’s pharmacy – colchicine pharmacy
I am sure this article has touched all the internet users, its really really fastidious article on building up new website.
Five Lessons You Can Learn From Private Psychiatrist Sheffield Cost how Much does a private psychiatrist cost
meijer pharmacy hours: domperidone online pharmacy – pharmacy shoe store
What’s The Job Market For Treadmill Best Professionals? treadmill best
A lot of Phase 13 plans need what does it mean to file a chapter 11 be 3
to five years long.
Если вы искали где отремонтировать сломаную технику, обратите внимание – профи челябинск
In this instance, the life insurance firm’s loan.
Also visit my webpage texas rustic wedding venues
propecia online pharmacy: best generic viagra pharmacy – humana pharmacy store
ретрит медитация https://ретриты.рф
Car Keys Repairs Tools To Ease Your Daily Lifethe One Car Keys Repairs Trick That Every Person Must Be Able To Mobile key fob repair
USP human development hormonal agent (somatropin).
Here is my blog post … orthopedic surgeon elbow specialist near me
Hi, of course this article is truly good and I have learned lot of things from it regarding blogging.
thanks.
Если вы искали где отремонтировать сломаную технику, обратите внимание – ремонт бытовой техники в челябинске
They have items to sell or solutions to supply.
Here is my homepage … Bookmarks
Inspiring story there. What happened after? Take care!
10 Things Everyone Has To Say About Volvo Replacement Key volvo v40 Key Replacement (https://claveping71.bravejournal.net)
pharmacy viagra prices uk: pharmacy escrow viagra – lorazepam pharmacy online
fluconazole target pharmacy: boots pharmacy diflucan – which online pharmacy is the best
The Most Successful Testing For ADHD Gurus Are Doing 3 Things free adhd test for Adults uk
Allow me reveal you in my evaluation of NewULife.
My web page shoulder doctors nyc
I know this site offers quality depending articles օr reviews аnd extra material,
іs there anny otһer web site whidh ρrovides these kinds of stuff іn quality?
Αlso visit my web-site; optik jakarta barat
https://telegra.ph/%EB%B9%84%EC%95%84%EA%B7%B8%EB%9D%BC-%EA%B5%AC%EB%A7%A4-%EC%98%A4%ED%94%84%EB%9D%BC%EC%9D%B8-%EC%95%BD%EA%B5%AD%EA%B3%BC-%EC%98%A8%EB%9D%BC%EC%9D%B8-%EC%87%BC%ED%95%91%EC%9D%98-%EC%9E%A5%EB%8B%A8%EC%A0%90-07-31
propecia online pharmacy uk: plavix online pharmacy – doxycycline people’s pharmacy
Профессиональный сервисный центр по ремонту фото техники от зеркальных до цифровых фотоаппаратов.
Мы предлагаем: ремонт проекторов в москве
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
in store pharmacy: buying percocet online pharmacy – buy medication without an rx
5 Killer Quora Answers To Fold Away Treadmill UK fold away treadmill uk
вывод из запоя ростов и область http://vyvod-iz-zapoya-rostov12.ru/ .
XYGENYX, a licensing company for FDA-registered products.
Have a look at my web-site … Replacement Windows Costco