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://viagracanada.xyz/# does viagra stop you coming
Hi! I know this is kind of off topic but I was wondering if
you knew where I could find a captcha plugin for my comment form?
I’m using the same blog platform as yours and I’m having trouble finding one?
Thanks a lot!
Excellent post. I was checking continuously this weblog and I
am inspired! Extremely useful information specially the remaining section :
) I deal with such information a lot. I was looking for this certain info for a very long time.
Thank you and best of luck.
This post will help the internet visitors for building
up new blog or even a blog from start to end.
Nice web page you’ve going here.
https://nasz-szczecin.pl
nolvadex for pct tamoxifen and osteoporosis
I love this website – its so usefull and helpfull.
https://www.kopalniapracy.pl
This article will assist the internet people for setting up
new blog or even a blog from start to end.
Also visit my web-site w88
Spot on with this write-up, I truly believe that this amazing site needs far more attention. I’ll probably be back again to read more, thanks for the information!
%%
Here is my homepage; big Five
%%
Also visit my homepage: tritype; Parthenia,
I wanted to thank you for this great read!! I certainly
enjoyed every bit of it. I have you book marked to check out new stuff you
post…
ทางเราจำหน่าย kardinal stick , ks quik ,
ks kurve ต้องขอบอกได้เลยว่า kardinalstealththailand.com เป็นตัวแทนหลักอย่างเป็นทางการในไทย ที่ใหญ่ที่สุด และเป็นเจ้าเดียวกับ RELX THAILAND สินค้าทุกแบรนด์ ทุกรุ่น เราได้ทำการคัดสรร
บุหรี่ไฟฟ้า ที่เป็นหนึ่งในนวัตกรรม ช่วยเลิกบุหรี่
ที่มีประสิทธิดีเยี่ยม และช่วยได้จริง
มาให้ลูกค้าได้เลือกใช้ โดยสินค้าทุกชิ้นของเรา สั่งตรงจากโรงงาน
I pay a quick visit everyday some blogs and information sites to
read articles or reviews, but this web site provides feature based content.
I’m not sure where you’re getting your information, but great
topic. I needs to spend some time learning
much more or understanding more. Thanks for magnificent information I was looking for this information for my mission.
She is eligible for re-promotion on a non-competitive basis.
My web blog; Rodrick
Today we will talk about drdo. Please ask me something
It’s remarkable to visit this web page and reading the views of all mates on the topic of this post, while I am also eager of getting know-how.
%%
Feel free to visit my web page: socionics
Have you ever considered creating an ebook or guest
authoring on other blogs? I have a blog centered on the same topics you discuss and would love to have you share some
stories/information. I know my visitors would appreciate your work.
If you are even remotely interested, feel free to send me an email.
Hey! 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 tips?
%%
Also visit my web page; togel – Jesenia,
can you get viagra over the counter do you need a prescription for female viagra
What’s up to all, since I am genuinely keen of reading this webpage’s post to be updated regularly.
It contains pleasant stuff.
Τhanks ѕo muсh regardіng gіving everyone an update on thіs ttopic ⲟn your website.
Ρlease understand tһat if ɑ brand new post appears ⲟr iin tһe event any modifications occur ᴡith tһe
current publication, Ι wouldd bee considering
reading ɑ lot mоre аnd learning һow t᧐ makе gokd
utilization oof tһose approɑches you tɑl abοut.
Thanks foг your efforts and consideration ߋf othеr individuals by makіng this website ɑvailable.
What a material of un-ambiguity and preserveness of valuable know-how
regarding unpredicted emotions.
Great internet site! It looks really professional!
Keep up the helpful work!
https://www.nowytydzien.pl
Right away I am ready to do my breakfast, when having my breakfast coming over again to read more news.
%%
my webpage; Live Result Hongkong Pools
My programmer is trying to persuade me to move to .net from PHP.
I have always disliked the idea because of the costs. But he’s tryiong none the less.
I’ve been using WordPress on numerous websites for
about a year and am concerned about switching to another platform.
I have heard fantastic things about blogengine.net. Is
there a way I can transfer all my wordpress posts
into it? Any kind of help would be really appreciated!