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
https://telegra.ph/Roloc-%E5%88%B7%E6%AF%9B%E7%9B%A43-5-8-%E9%8C%90%E5%BD%A2%E5%88%B7%E6%AF%9B%E7%9B%A4-11-29-15
Usually a pleasant knee length dress is ideal to beat the hot weather.
Ԝhat’s up mates, how is the whole thing, and what yoᥙ deѕire to say regarding this p᧐st, in my view іts actually awesome
in support of me.
my blⲣog :: discuss
Wow, superb blog layout! How long have you been blogging for?
you make blogging look easy. The overall look of your web site is
magnificent, as well as the content!
https://bogin94.z19.web.core.windows.net/research/bogin94-(76).html
A twinset can have a “fuddy duddy” status, however it undoubtedly doesn’t should look quaint.
https://bogin79.z12.web.core.windows.net/research/bogin79-(35).html
Whether you may have your heart on embroidery, embellishment, sequin, or ruched silk smoothness, golden dresses look great in all types and designs.
continuously i used to read smaller articles that as well clear
their motive, and that is also happening with this
paragraph which I am reading now.
For newest information you have to pay a visit world-wide-web and on the web I found this site as a finest site for
most up-to-date updates.
I feel that is among the so much significant information for me.
And i am glad studying your article. But want to observation on some common issues, The website taste is perfect,
the articles is in point of fact excellent : D. Good job, cheers
Have you ever thought about publishing an e-book or guest authoring on other sites?
I have a blog based upon on the same topics you discuss and would really like to have you share
some stories/information. I know my viewers would value your work.
If you’re even remotely interested, feel free to send me
an email.
Piece of writing writing is also a fun, if you be familiar with
after that you can write otherwise it is difficult to write.
Great post.
Heгe is my site :: slot (Stormy)
I like the valuable information you provide to
your articles. I’ll bookmark your blog and take a look at again here frequently.
I am rather certain I will learn many new stuff right right here!
Good luck for the next!
І ҝnow tһis if off topic Ьut Ӏ’m looking
into starting my own weblog and wɑs curious what ɑll is required tо get setup?
I’m assuming һaving a blog like ʏοurs woᥙld
cost a pretty penny? I’m not νery web smart sօ Ӏ’m not
100% sᥙre. Any tips or advice woսld bе
gгeatly appreciated. Αppreciate it
Here is my web рage lotto – Bennie –
If you wish for to improve your familiarity only
keep visiting this website and be updated with the
most recent news update posted here.
Since the admin of this website is working, no hesitation very soon it
will be famous, due to its feature contents.
Here is my homepage :: binary options
If you want to take a good deal from this article then you have to apply
such techniques to your won web site.
Generally I don’t read post on blogs, but I wish to say that this write-up
very forced me to take a look at and do so! Your writing taste has been surprised me.
Thank you, quite great article.
asbestos mesothelioma legal claim – Bettina,
%%
My web site organic Cbd oil for sale (cbd-organic-store66992.free-blogz.com)
Your means of describing everything in this post
is in fact good, all be capable of effortlessly understand it, Thanks a lot https://www.lescaledetente.com/massage-suedois/
When someone writes an paragraph he/she keeps the image of a user
in his/her brain that how a user can understand it. Therefore that’s why this
post is great. Thanks!
how long Do mesothelioma claims take
railroad cancer lawsuit (Lena)
how to file a mesothelioma claim (Maricruz)
%%
Feel free to visit my blog – male anal toy (https://forum.giperplasma.ru/index.php?action=profile;u=689771)
Hurrah, that’s what I was exploring for, what a material!
present here at this website, thanks admin of this site.
mesothelioma Attorney wisconsin – yogaasanas.Science,
Are asbestos lawsuit settlements Taxable
I will right away seize your rss as I can’t to find your email subscription link or newsletter service.
Do you’ve any? Kindly permit me realize so that I could
subscribe. Thanks.
Visit my blog Eco-Friendly Tiny House