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
Hello mates, its wonderful article regarding cultureand entirely explained,
keep it up all the time.
safe list of darknet market links steroid market darknet
%%
My webpage; how to get Investors
darknet market updates 2022 black market websites 2022
THC isolate is pure THC gummies france,
with no other cannabinoids.
%%
My blog … glass replacement near me
Its such as you read my thoughts! You appear to grasp so much approximately this,
such as you wrote the book in it or something. I believe that you could do with some % to force the message house a bit, but instead of that,
this is excellent blog. A fantastic read. I will certainly be back.
%%
Feel free to surf to my web site; sexdoll Sale
%%
my webpage: muscle Cbd Balm
Sports betting. Bonus to the first deposit up to 500 euros.
Online Casino.
sports betting
%%
Here is my homepage: buy Cbd high strength online uk
cp onion deep web deb
%%
Take a look at my page – Cbd Capsules For Sale Near Me
%%
Also visit my page – how to find investors in south africa (Mellissa)
I have been browsing online more than 3 hours today, yet I
never found any interesting article like yours.
It’s pretty worth enough for me. In my view, if all site owners and bloggers made good content as you did, the
net will be much more useful than ever before.|
I could not refrain from commenting.
Perfectly written!|
I will right away grasp your rss feed as I can not to find your email subscription link or
newsletter service. Do you have any? Please allow me recognize in order
that I may just subscribe. Thanks. |
It is appropriate 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 can write next articles referring to this article.
My spouse and I stumbled over here from a different page and thought I might as
well check things out. I like what I see so now i am following you.
Look forward to finding out about your web page again.
onion domain and kingdom drugs on the deep web
%%
my web site – Cbd Oil Sweets Uk
Definitely imagine that that you stated. Your favorite justification appeared to be at the web the simplest
thing to take note of. I say to you, I definitely get annoyed while folks think
about worries that they just don’t recognize about. You controlled to hit the nail upon the top and defined out the
entire thing without having side-effects , people can take
a signal. Will probably be again to get more. Thanks
%%
my blog … list of investors in South africa
Hi to all, how is everything, I think every one is getting more from this web page, and your
views are fastidious in favor of new users.
bactrim without prescription can we buy amoxcillin 500mg on ebay without prescription
It’s appropriate time to make some plans for the future and
it is time to be happy. I have read this post and if I could I wish to
suggest you some interesting things or tips.
Maybe you can write next articles referring to this article.
I want to read even more things about it!
Take a look at my page; avon kosmetik online shop, Madie,
%%
Here is my web site: broken glass repair – Madge,
You Need Roofing Contractors?
I am regular reader, how are you everybody? This article posted at this web page is actually good.
And also the range of various THC gummies montreal items.
https://wincollection.net/
74cd785c74 ventvoyt
If you are going for finest contents like me, simply pay a visit this web
site every day for the reason that it offers feature contents, thanks
my website; avon shop online (rpoforums.com)
darknet black market sites tor2door market