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
%%
Take a look at my site; Glazing Repairs High Wycombe
мега купить даркнет ссылки
underground hackers black market dark net market list reddit
black market drugs guns best darknet market may 2022 reddit
underground hackers black market onion market url
darknet link drugs versus market darknet
darknet market redit darknet market arrests
%%
My homepage :: door repairs romford
dnm xanax underground website to buy drugs
Hmm is anyone else experiencing problems with the images on this blog loading?
I’m trying to figure out if its a problem on my end or if it’s the blog.
Any suggestions would be greatly appreciated.
Stop by my web blog :: BioLife Keto Gummies
french dark web reddit darknet market uk
%%
Also visit my website; double glaze windows – Beatriz –
bohemia market darknet vice city link
what is the best darknet market dark markets croatia
onion directory list darknet new market link
how to get on the dark web android dark markets philippines
how to shop on dark web dark markets italy
%%
Feel free to visit my web site – composite doors high wycombe [Ignacio]
Good day! Do you know if they make any plugins to safeguard against
hackers? I’m kinda paranoid about losing everything I’ve worked hard on. Any suggestions?
%%
Here is my website – Repairing Upvc Windows
%%
Here is my web page – leather Flight jackets (http://Www.adsmos.com)
зайти на мегу mega onion зеркала
%%
my website sexdoll – Jaunita –
зеркало мега mega darknet
Fantastic goods from you, man. I have take into accout your stuff previous to
and you’re simply too fantastic. I actually like what you’ve got right here, certainly like what you’re saying and the way in which by which you say it.
You are making it enjoyable and you continue to take care of to keep it wise.
I cant wait to learn much more from you. That is really a terrific website.
My homepage :: playing online
%%
Feel free to visit my blog post; front door Replacement
%%
Feel free to surf to my blog post: replacement double Glazing
ссылка на мегу зайти на мегу
%%
Here is my web blog; Gambler
Helⅼо, thee wholee tһin iis goinng nicey ere annd ofcourse evvery onee iis
sharing fɑcts, tһat’s iin factt good, keep upp writing.