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
you’re truly a good webmaster. The website loading pace
is incredible. It seems that you are doing any unique trick.
In addition, The contents are masterwork. you have performed a fantastic job in this subject!
Here is my homepage; prefab steel buildings
darknet drug store tor marketplace
%%
Here is my website :: 510 Cbd cartridges
tor markets 2022 dark web market links
tor darknet dark web sites links
It is legal to play poker online, but be sure that you’re legally able to play.
Poker sites online require that players are at least 18 years
old to play. However, some sites may require a verification process before
you can download their software.
my homepage – bet – Riley,
darkmarket link tor markets links
drug markets onion dark websites
best darknet markets darknet market lists
%%
Take a look at my page … top ten onlyfans (Penelope)
A winning Powerbal ticket worth about $731 million was sold
in Lonaconing, a modest town in Western Maryland’s Allegany
County.
Here is myy blog: Go here
darknet market list onion market
dark web drug marketplace dark web market list
darkweb marketplace dark market link
Thanks for sharing your info. I truly appreciate your efforts and I
will be waiting for your next post thank you once again.
Look into my page baccarat
deep dark web darknet markets
dark market link deep web markets
dark web sites darknet marketplace
tor markets 2022 tor market
The information shared is of top quality which has to get appreciated at all levels. Well done…
the canadian drugstore prescription drugs without doctor approval
An outstanding share! I’ve just forwarded this onto a co-worker who has
been conducting a little research on this.
And he in fact bought me lunch because I stumbled upon it for him…
lol. So allow me to reword this…. Thank YOU for the meal!!
But yeah, thanx for spending the time to talk about this topic
here on your website.
Hi there, You’ve done an excellent job. I’ll definitely digg
it and personally recommend to my friends. I’m sure they will
be benefited from this site.
Feel free to surf to my web-site; bookmarks
what is stromectol https://stromectol.beauty
dark market link dark web markets
tor market url darknet site
There are a variety of treatments for addiction to gambling.
Some of them involve one-on one counseling, medication, and lifestyle changes.
If you can’t stop yourself from engaging in this
behaviour it can become an addiction.
Here is my web-site bet
Excellent post. I was checking constantly this blog and I am impressed!
Extremely helpful information specially the last part 🙂 I care for such info a lot.
I was seeking this particular info for a long time.
Thank you and best of luck.
tor markets 2022 darknet drug market
%%
my homepage … Best Cbd Cartridge