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
%%
Also visit my webpage – boat injury Lawsuit
Hi, I log on to your blogs regularly. Your humoristic style is witty, keep it up!
My blog post: shopping avon online – http://dino-farm.kr/bbs/board.php?bo_table=gallery&wr_id=596626,
dark markets colombia Cocorico Market link
мега купить соль сайты даркнет
%%
Review my web blog – Sexdoll For Sale
darknet drug vendor that takes paypal biggest darknet markets
reddit onion list deep web marketplaces reddit
I quite like reading an article that will make people think.
Also, many thanks for permitting me to comment!
My webpage; Avon Uk Shop
adresse onion dark markets bolivia
mega darknet сайты даркнет ссылки
%%
Here is my web page; upvc door replacement panel
%%
My webpage – cost to Replace windows uk
зеркало мега мега онион
monkey xtc pill bitcoins and darknet markets
tor link search engine cypher darknet market
reddit darknetmarket ketamine darknet market
mega darkmarket mega онион
darknet illegal market dark web sales
how to buy from the darknet markets dark web market list
%%
Feel free to visit my blog post :: Bifold doors Ilford
https://toolbarqueries.google.com.tr/url?q=https://maywoodchamber.com/best-vibrating-panties-with-remote-control/
%%
Here is my blog post; residential locksmith in horsham
darknet market and monero reddit darknet market noobs bible
darknet search counterfeit euro deep web
tor marketplace darknet credit card market
It’s actually a nice and useful piece of info.
I am happy that you shared this useful information with us.
Please keep us up to date like this. Thank you for sharing.
%%
My site; Doors High Wycombe
https://installzip.com/
74cd785c74 jashans
мега официальный сайт мэги сайт
First off I would like to say wonderful blog!
I had a quick question which I’d like to ask if you don’t
mind. I was curious to find out how you center yourself and clear
your mind prior to writing. I’ve had a difficult time clearing my mind in getting my ideas
out there. I do enjoy writing but it just seems like the first 10 to 15 minutes are generally
wasted simply just trying to figure out how to begin. Any suggestions or hints?
Thank you!
Feel free to surf to my web blog 먹튀검증