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
лечение наркозависимости в стационаре zarabotokdoma.creartuforo.com/viewtopic.php?id=11483 .
вывод из запоя воронеж http://zal.rolevaya.info/viewtopic.php?id=5384/ .
It’s the best time to make some plans for the future and it’s time to be happy.
I have read this post and if I could I wish to suggest you some interesting things or suggestions.
Perhaps you could write next articles referring to this article.
I want to read even more things about it!
вывод из запоя стационар http://www.obovsem.rolevaya.info/viewtopic.php?id=3681 .
выведение из запоя воронеж стационар https://gaslo.ukrbb.net/viewtopic.php?f=13&t=3413 .
Very informative post! Looking to have some work done on my pool enclosure; thinking of calling Swimming Pool Screen Repair
You said it nicely.!
вывод из запоя стационар http://zarabotokdoma.creartuforo.com/viewtopic.php?id=11483 .
лечение наркозависимости в стационаре лечение наркозависимости в стационаре .
Пошаговая инструкция по безопасной покупке диплома о высшем образовании
It’s very easy to find out any matter on net as compared to books, as I found this piece of
writing at this website.
выведение из запоя воронеж стационар выведение из запоя воронеж стационар .
Також передбачені часове та просторове
шумозаглушення, додавання зернистості, розмиття, імли та інших ефектів.
вывод из запоя воронеж http://www.justforum.bestforums.org/viewtopic.php?f=26&t=4753 .
Pretty section of content. I just stumbled upon your web
site and in accession capital to assert that
I get actually enjoyed account your blog posts. Any way I’ll be subscribing to your feeds and even I achievement you access consistently quickly.
вывод из запоя воронеж http://www.ideya.forums.party/viewtopic.php?id=661 .
вывод из запоя воронеж http://planeta.mybb.social/viewtopic.php?id=2229 .
выведение из запоя воронеж стационар http://rio16.ukrbb.net/viewtopic.php?f=3&t=1120/ .
Having read this I thought it was extremely enlightening.
I appreciate you spending some time and effort to put this informative article together.
I once again find myself spending a significant amount of time both reading
and leaving comments. But so what, it was still worth it!
Feel free to visit my webpage; sell your Van
вывод из запоя стационар http://www.rolandus.org/forum/viewtopic.php?p=106439 .
dark web search engine darknet marketplace dark market
plavix best price [url=https://plavixclo.com/#]PlavixClo Best Price[/url] Plavix 75 mg price
вывод из запоя в стационаре http://www.superjackson.ukrbb.net/viewtopic.php?f=28&t=9734/ .
Pills information for patients. Cautions.
where can i buy cheap thorazine online
Some trends of drugs. Get here.
search engine marketing is an important for visibility in contemporary marketplace SEO Agency
Some truly interesting information, well written and
broadly speaking user friendly.
выведение из запоя воронеж стационар https://superjackson.ukrbb.net/viewtopic.php?f=28&t=9734/ .
вывод из запоя в стационаре [url=https://justforum.bestforums.org/viewtopic.php?f=26&t=4753/]https://justforum.bestforums.org/viewtopic.php?f=26&t=4753/[/url] .
вывод из запоя воронеж superjackson.ukrbb.net/viewtopic.php?f=28&t=9734 .
This paragraph will assist the internet visitors for creating new weblog or even a weblog from start to end.