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
Does CBD Affect Brain Development For
Treating Drastic Emotional Mood Swings
First off I would like to say fantastic blog!
I had a quick question that I’d like to ask if you don’t mind.
I was interested to know how you center yourself and clear
your thoughts before writing. I have had trouble clearing my
mind in getting my ideas out there. I truly do enjoy writing but it just seems like the first 10 to 15 minutes are usually lost simply
just trying to figure out how to begin. Any suggestions or tips?
Thank you!
%%
My web page :: togel hk hari ini
ทางเราจำหน่าย kardinal stick , ks quik , ks
kurve ต้องขอบอกได้เลยว่า kardinalstealththailand.com เป็นตัวแทนหลักอย่างเป็นทางการในไทย ที่ใหญ่ที่สุด และเป็นเจ้าเดียวกับ RELX THAILAND สินค้าทุกแบรนด์ ทุกรุ่น เราได้ทำการคัดสรร บุหรี่ไฟฟ้า ที่เป็นหนึ่งในนวัตกรรม
ช่วยเลิกบุหรี่ ที่มีประสิทธิดีเยี่ยม และช่วยได้จริง มาให้ลูกค้าได้เลือกใช้ โดยสินค้าทุกชิ้นของเรา สั่งตรงจากโรงงาน
Thank you for sharing your info. I really appreciate your efforts and I am waiting for your further post thanks once again.
Hi it’s me, I am also visiting this site on a regular basis, this site is genuinely
nice and the viewers are truly sharing pleasant thoughts.
Link exchange is nothing else but it is simply placing the other person’s webpage link on your page at proper place and other person will also do
similar in support of you.
Quite helpful, looking forth to returning.
https://mojmikolow.pl
Greate post. Keep writing such kind of info on your page.
Im really impressed by it.
Hey there, You have performed an excellent job. I’ll definitely digg it and in my view recommend to my friends.
I am confident they will be benefited from this web site.
%%
my webpage :: Data Hk
Fantastic items from you, man. I have be aware your stuff prior to and
you are simply extremely wonderful. I really like what you’ve acquired here, certainly
like what you’re saying and the way by which you are saying it.
You make it entertaining and you continue to take care of to stay
it smart. I cant wait to read far more from
you. That is actually a tremendous site.
Also visit my web site: w88
Nice post. I learn something totally new and challenging on sites
I stumbleupon on a daily basis. It’s always useful to read content from other authors and practice
something from their sites.
molnupiravir antiviral antiviral drug molnupiravir
It’s actually a nice and helpful piece of info. I am satisfied that
you just shared this useful info with us. Please stay us informed like this.
Thank you for sharing.
Wonderful blog! I found it while searching on Yahoo News.
Do you have any tips on how to get listed in Yahoo
News? I’ve been trying for a while but I never seem to get there!
Thank you
Passion the site– very user pleasant and whole lots to see!
https://www.kariera24.info
Good day! This is kind of off topic but I need some guidance from an established blog.
Is it very difficult to set up your own blog? I’m not very techincal but I can figure things out pretty
fast. I’m thinking about making my own but I’m not sure where to start.
Do you have any tips or suggestions? Thanks
You’ve got one of the greatest internet websites.
http://www.kurier-lokalny.com
Do you have a spam problem on this site; I also am a blogger, and I was curious about your situation; we have created
some nice methods and we are looking to swap
methods with other folks, why not shoot me
an e-mail if interested.
I am sure this article has touched all the internet users,
its really really fastidious piece of writing on building up new web site.
The information shared is of top quality which has to get appreciated at all levels. Well done…
I used to be able to find good info from your blog articles.
Hello to all, how is everything, I think
every one is getting more from this website, and your views are pleasant for new people.
Take a look at my webpage – minecraft coloring pages printable
For latest information you have to visit world-wide-web and on the web I found this web page as a best web site
for hottest updates.
What’s up, after reading this remarkable piece of writing i am as well cheerful to share my familiarity here with
colleagues.
Definitely imagine that that you said. Your favorite reason seemed to be at the web the easiest thing to consider of.
I say to you, I definitely get irked while people think about issues that
they plainly do not understand about. You controlled to hit the nail upon the highest as
well as outlined out the entire thing with no need side-effects
, other people can take a signal. Will probably be again to get more.
Thank you
https://paxlovid.best/# when will paxlovid be available
I gotta favorite this site it seemѕ very useful very helpful.
https://tamoxifen.best/# nolvadex pills