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
No matter if some one searches for his necessary thing,
so he/she wishes to be available that in detail, thus
that thing is maintained over here.
Howdy! Thіs blog post could not Ьe ԝritten any Ƅetter!
Looking at tһis post reminds me of my previouѕ roommate!
He always kеpt preaching abⲟut this. I am going tօ ѕend tһis
post to him. Pretty sսre he’s going t᧐ haѵe a greɑt
read. I appreciate you fоr sharing!
Ꮇy web blog lotto (Jina)
%%
Check out my webpage :: Asbestos Settlement
mesothelioma lawyers houston texas (http://worksale.nnov.org)
Kamagra tablets Kamagra 100mg price cheap kamagra
Write more, thats all I have to say. Literally, it seems as though
you relied on the video to make your point.
You clearly know what youre talking about, why throw away your intelligence
on just posting videos to your blog when you could be giving us something
enlightening to read?
%%
Here is my page Scooters
asbestos mesothelioma lawyers
%%
Stop by my web page – Certified Online Jobs Work From Home (Away.Vk.Com)
%%
Also visit my web page – avon shop with my rep; Orval,
%%
my web-site – subaru key fob
Hi, i think that i saw you visited my site thus i came to “return the favor”.I’m trying
to find things to improve my site!I suppose its ok to use some of your ideas!!
I’m very happy to uncover this page. I want to
to thank you for your time just for this wonderful read!!
I definitely savored every part of it and i also have you
book-marked to look at new things in your website.
I am no longer positive the place you’re getting your info, but good topic.
I must spend a while finding out much more or figuring out more.
Thanks for magnificent information I used to be on the lookout for this information for my
mission.
%%
Also visit my web site – Auto Locksmithing
%%
My web page; door fitters Chelmsford
Please let me know if you’re looking for a writer for your site.
You have some really good posts and I believe I would be a good asset.
If you ever want to take some of the load
off, I’d love to write some material for your blog in exchange for a
link back to mine. Please shoot me an e-mail if interested.
Thanks!
Helpful information. Fortunate me I discovered your website by chance, and
I’m surprised why this twist of fate did not happened earlier!
I bookmarked it.
Hi, after reading this awesome paragraph i am too delighted to share my experience here with colleagues.
%%
Here is my site – asbestos
%%
my blog – work from home Jobs online
upvc casement windows bishops stortford (yogaasanas.science)
%%
Here is my blog post :: locksmith near me Car Keys
%%
Feel free to surf to my web page; door Fitters Near me
%%
Feel free to visit my webpage: buy second hand mobility scooter [Kristine]
Very nice article, totally what I was looking for.
%%
Also visit my site; representative sales [Mavis]
%%
my web blog … asbestos compensation (Felipa)
mesothelioma Class Action Settlement
%%
my web blog: Best Over Ear Headphones Without Noise Cancelling