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
Portugal has long been a country known for producing exceptional talent in the world of sports. With a rich history in football, many athletes from this nation have achieved international fame through their incredible skills and accomplishments http://letmefix.lt/index.php?title=_The_Environmental_Impact_of_Training_LGPT_Models
выведение из запоя на дому выведение из запоя на дому .
I know this website gives quality depending posts and
additional information, is there any other site which offers these stuff in quality?
Unexpected Business Strategies That Helped Upvc Door Locks Achieve Success replacement Upvc Door lock
This is my first time pay a quick visit at here and i am actually pleassant to read everthing at alone place.
en kazancl? slot oyunlar? en cok kazand?ran slot oyunlar? en kazancl? slot oyunlar?
вывод из запоя анонимно вывод из запоя анонимно .
вывод из запоя наркология вывод из запоя наркология .
вывод. из. запоя. ростов. https://www.vyvod-iz-zapoya-rostov229.ru .
вывод из запоя цены ростов-на-дону вывод из запоя цены ростов-на-дону .
вывод из запоя в ростове вывод из запоя в ростове .
Fantastic read! If you’re in Canoga Park and need AC assistance, I quite recommend finding out https://blast-wiki.win/index.php/Calabasas_HVAC_Contractor_for_All_Your_Needs for trained directions
вывод из запоя недорого ростов вывод из запоя недорого ростов .
At this time I am going to do my breakfast, after having my breakfast coming yet again to read additional
news.
Canl? Casino Siteleri: Casino Siteleri – Casino Siteleri
en kazancl? slot oyunlar? en kazancl? slot oyunlar? en cok kazand?ran slot oyunlar?
slot oyunlar? puf noktalar?: slot casino siteleri – slot oyunlar? puf noktalar?
https://sweetbonanza25.com/# sweet bonanza guncel
casino bahis siteleri: casino bahis siteleri – en guvenilir casino siteleri
See What Local SEO Companies Near Me Tricks The Celebs Are
Making Use Of Local seo companies near me (https://Ralston-bennett.technetbloggers.de/why-is-this-local-seo-companies-near-me-so-beneficial-in-covid-19/)
I was curious if you ever thought of changing the layout of your website?
Its very well written; I love what youve got to say. But maybe you
could a little more in the way of content so people could connect with it better.
Youve got an awful lot of text for only having 1 or 2 images.
Maybe you could space it out better?
slot oyunlar? puf noktalar? slot siteleri az parayla cok kazandiran slot oyunlar?
поставить капельницу от запоя на дому цена поставить капельницу от запоя на дому цена .
вывод из запоя на дому недорого вывод из запоя на дому недорого .
нарколог на дом вывод из запоя ростов нарколог на дом вывод из запоя ростов .
Cocktail Lounge 광주휴게텔
en guvenilir casino siteleri Canl? Casino Siteleri canl? casino siteleri
You actually make it seem so еasy with
your presentation but I find this topic to be really something which I
think I would never understand. It seems too complex and very
broad for me. I’m looking forward fߋr your next post, I will try to get the hɑng of it!
Alѕⲟ visit my blog post; inmate texting
I’ve been struggling with bed bugs for months. This article gave me some hope! For further guidance, check out termite control near me
Now I am ready to do my breakfast, once having my breakfast coming yet again to read further news.