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
I am curious to find out what blog platform you have been working with?
I’m having some small security problems with my latest website and
I would like to find something more safe. Do you have any solutions?
deep web drug markets bitcoin dark web best darknet markets
https://iverfast.com/# order minocycline 100mg online
buy priligy: max pharm – cheap priligy
پوستیژ مو
Диплом пту купить официально с упрощенным обучением в Москве
Hello to every one, the contents present at this web page are truly awesome for people experience, well, keep up the nice work
fellows.
Excellent article! We will be linking to this great content on our site.
Keep up the good writing.
An compelling discussion is worth commentary. There’s no doubt that that you should write read more about this topic, it might not be a taboo subject but generally people don’t discuss so. To the next! Kind regards!!
my web site :: http://Oople.com/forums/member.php?u=246522
Полезные советы по безопасной покупке диплома о высшем образовании
Пошаговое руководство по восстановлению прозрачности фар с помощью наждачной бумаги разной зернистости. Советы по выбору материалов и технике обработки https://aggam.xyz/service/5-glavnyh-oshibok-pri-vybore-nazhdachnoj-bumagi-dlya-polirovki-avtomobilnyh-far/
generic amoxicillin over the counter: cheap amoxil – amoxicillin 500 mg tablets
выведение из запоя воронеж стационар https://kvitka.ukrbb.net/viewtopic.php?f=58&t=28033 .
http://iverfast.com/# ivermectin oral 0 8
Higly Demand of Durian Land
cheap priligy: priligy maxpharm – buy priligy
Guide To Metal L Shaped Bunk Beds: The Intermediate Guide On Metal L Shaped Bunk Beds metal l shaped bunk beds (Katja)
вывод из запоя в стационаре https://alhambra.bestforums.org/viewtopic.php?f=2&t=46417 .
I have read several good stuff here. Definitely value bookmarking for revisiting.
I wonder how so much attempt you put to create such a magnificent informative site.
5 Reasons To Be An Online Fleshlight For Men Business And
5 Reasons To Not Best Of Fleshlight
Land price always will increase because of demands
An compelling conversation is worth commentary. I believe is that you need to write find out more on this particular topic, it may not be a taboo topic but generally people don’t discuss so. To the next! Best wishes!!
Check out my webpage :: http://www.Adtgamer.com.br/showthread.php?p=498395
Hi, its pleasant article concerning media print, we all know media is a great source of facts.
Good tips Many thanks!
Everything is very open with a clear description of the challenges.
It was really informative. Your website is extremely helpful.
Thank you for sharing!
вывод из запоя воронеж вывод из запоя воронеж .
I’m not that much of a internet reader to be
honest but your sites really nice, keep it up! I’ll go ahead and
bookmark your website to come back down the
road. All the best
Swedish Massage 하이오피
cheap plavix antiplatelet drug: buy plavix online – buy plavix
Happy Hour 대구유흥 – http://aragaon.net/bbs/board.php?bo_table=review&wr_id=1158262,