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
Undeniably imagine that that you said. Your favourite justification seemed to be at
the internet the easiest thing to understand of. I say to you, I
definitely get irked at the same time as other people consider issues that
they just do not know about. You managed to hit the nail
upon the top as neatly as outlined out the whole thing with no need side-effects ,
other people could take a signal. Will probably be back to get more.
Thank you
вавада казино зеркало вавада казино вавада казино онлайн
Как безопасно купить диплом колледжа или ПТУ в России, что важно знать
Great looking site. Presume you did a whole lot of your own html coding.
laziska.com.pl
how to buy zestril prices
1с предприятие базовая цена 1с предприятие базовая цена .
Аттестат 11 класса купить официально с упрощенным обучением в Москве
Thanks for sharing such a nice thinking, post is fastidious, thats why i have read it fully
пин ап казино онлайн: pinup-kazi.kz – pinup kazi
Someone necessarily assist to make significantly articles I would state.
This is the first time I frequented your website page and thus far?
I amazed with the research you made to create this actual
put up incredible. Magnificent process!
Here is my homepage – box set movies
Как оказалось, купить диплом кандидата наук не так уж и сложно
vavada: vavada kazi – вавада казино
вавада казино зеркало: vavada kazi – vavada
https://pinup-kazi.ru/# pinup kazi
Its like you read my mind! You appear to know a lot about this, like you wrote the book in it or something.
I think that you can do with some pics to drive the message home a
bit, but instead of that, this is wonderful blog.
A fantastic read. I’ll definitely be back. http://Qzfczs.com/comment/html/?525101.html
пинап казино: pinup – пин ап казино официальный сайт
Процесс получения диплома стоматолога: реально ли это сделать быстро?
Excellent article. Keep posting such kind of info on your page.
Im really impressed by your blog.
Hi there, You’ve done an excellent job. I’ll definitely digg it and individually recommend to my friends.
I’m confident they’ll be benefited from this website.
Sweet blog! I found it while surfing around on Yahoo News.
Do you have any tips on how to get listed in Yahoo News?
I’ve been trying for a while but I never seem to get there!
Many thanks
Официальная покупка диплома ПТУ с упрощенной программой обучения
pinup-kazi.ru: пин ап казино – пин ап вход
лечение запоев на дому http://www.domsadremont.ukrbb.net/viewtopic.php?f=3&t=907/ .
выведение из запоя врачом наркологом http://www.ekonomimvmeste.ukrbb.net/viewtopic.php?f=14&t=65376/ .
https://pinup-kazi.kz/# pinup
пин ап казино: пинап казино – пин ап вход
You’ve inspired me to reconsider my patio area entirely with those leading edge techniques round hardscaping—can’t wait to get begun!! excavation companies Concord
Gran guía sobre cómo evitar problemas legales Página de inicio
You are so awesome! I do not think I’ve read through
anything like this before. So nice to find someone with genuine thoughts on this subject matter.
Seriously.. many thanks for starting this up. This website is one thing that is required on the
web, someone with a bit of originality!
my web page nsk dental handpiece
пин ап кз pinup пинап казино
I believe that is one of the so much important info for
me. And i am happy studying your article. But should statement on some common things, The website style is ideal, the articles is in reality great : D.
Just right activity, cheers