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
http://pinup-kazi.kz/# pinup-kazi.kz
Many thanks! This a awesome web-site.
https://www.kopalniapracy.pl
Aus diesem Grund haben wir YouTube Video Downloader entwickelt.
вавада: вавада онлайн казино – вавада
The price of cheap car insurance coverage in Pasadena TX may differ, yet along with some investigation, it is actually effortless to locate affordable rates best auto insurance
скачать мобильный покер клуб
мобильный покер клуб на деньги
пин ап казино онлайн: pinup-kazi.kz – пин ап казино онлайн
Thanks designed for sharing such a fastidious thinking, piece
of writing is pleasant, thats why i have read it entirely
My web site … dvds for sale
I’m more than happy to find this great site.
I need to to thank you for your time due to this fantastic read!!
I definitely loved every part of it and I have you saved to
fav to check out new things on your website.
Stop by my website latest dvd releases
jackpoker review
https://t.me/s/jackpoker_review
пин ап казино pinup kazi пин ап зеркало
We stumbled over here by a different website and thought I might as well check things out.
I like what I see so i am just following you. Look forward
to looking into your web page for a second time.
Hiya, excellent web site you’ve right now.
https://www.tvzachod.pl
пин ап казино pinup kazi пин ап вход
darknet sites darknet market list darkmarkets
вавада: вавада казино зеркало – вавада казино зеркало
Great beat ! I would like to apprentice at the same time
as you amend your web site, how could i subscribe for a weblog
site? The account aided me a acceptable deal.
I were a little bit acquainted of this your broadcast provided bright transparent idea
My web blog – dvd distributors
В случае возникновения проблем клиенты казино могут в любое время обратиться в круглосуточную техническую поддержку.
Very good web-site you have here.
https://radomsko24.pl
pinup kazi pinup kazi пинап казино
Attractive element of content. I just stumbled upon your weblog and in accession capital to say that I get in fact enjoyed account your blog posts.
Any way I will be subscribing in your augment and even I achievement you
get entry to persistently quickly.
Hitclub ra mắt sân chơi mới thật hấp dẫn Hit Club
Демоверсия аппаратов в клубе Vulkan Russia —
отличный шанс поближе познакомиться с
играми, оценить показатель возврата и другие параметры.
Полезные советы по безопасной покупке диплома о высшем образовании
Быстрое обучение и получение диплома магистра – возможно ли это?
I have read so many articles regarding the blogger
lovers however this paragraph is genuinely a nice article, keep it up.
пин ап казино онлайн: пин ап казино – пинап казино
вавада казино зеркало: vavada – vavada kazi
cheap gabapentin for sale
казино вавада vavada вавада казино онлайн