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
I have Ьeen exploring for a littⅼе for any high-quality articles օr weblog posts ᧐n this kind ᧐f housde .
Exploring іn Yahoo I eventually stumbled upоn this site. Reading tһіs info So i
am haрpy to sһow that I have a vеry excellent uncanny feeling Ӏ fⲟund outt juѕt
what I needed. I sսch a ⅼot surely will mаke
sure to don?t put out ⲟf your mind tһis website and provіdes it ɑ look regularly.
my site: jasa view dan like youtube
Howdy just wanted to give you a brief heads up and
let you know a few of the pictures aren’t loading correctly.
I’m not sure why but I think its a linking issue.
I’ve tried it in two different web browsers and both show the same results.
I like reading an article that can make people think.
Also, thanks for allowing for me to comment!
My page; online poker ranking
%%
My blog: volvo keys (Holly)
Are you looking to buy a verified Cash App account?
Look no further! Verified accounts offer added security and benefits,
allowing you to send and receive money seamlessly.
Don’t waste time, get your hands on a verified Cash App account today!
Can you tell us more about this? I’d want to find out more details.
Всем рекомендую!английский язык онлайн
I’ve read several excellent stuff here. Definitely price bookmarking for
revisiting. I surprise how so much attempt you set to create this kind of excellent informative site.
This is the right site for everyone who wishes to understand this topic.
You understand so much its almost hard to argue with you (not
that I really would want to?HaHa). You certainly put a brand new spin on a topic
that has been discussed for ages. Wonderful stuff,
just great!
Also visit my page :: facebookofsex
Kamagra 100mg price: Kamagra Oral Jelly – Kamagra tablets
Wow, this paragraph is nice, my younger sister is analyzing these kinds of things, therefore I
am going to let know her.
Truly lots of awesome tips.
I believe that is one of the such a lot vital
info for me. And i’m glad reading your article. But want to commentary on some normal things, The website taste is ideal, the articles is truly great : D.
Excellent job, cheers
Here is my web-site top poker sites
door specialists basingstoke, http://gitlab.sleepace.com,
%%
Feel free to surf to my website … analyze a quote
%%
Also visit my website … Renault Clio Key (Baaraplus.Com)
Everything is very open with a very clear explanation of the challenges.
It was truly informative. Your website is extremely helpful.
Many thanks facebook for sex sharing!
Hello There. Ӏ discovered yⲟur weblog usingg msn. Ꭲhis іѕ а
reaⅼly neatly written article. Ι will ƅe sre tо bookmark іt and
return tо reaɗ extra of your usеful info. Ƭhank you for thе post.
I wll definiteⅼy return.
Visit my blog post views instagram
I love your blog.. very nice colors & theme. Did you design this website
yourself or did you hire someone to do it for you?
Plz respond as I’m looking to construct my own blog and would like to find out where u got this from.
thank you
Hi there Dear, are you truly visiting this website on a regular basis, if
so afterward you will definitely take pleasant experience.
%%
Also visit my web page: volkswagen key replacement near me – Addie –
It’s going to be end of mine day, but before finish I am reading this enormous paragraph to improve
my know-how.
Hi, i think that i saw you visited my weblog so
i came to “return the favor”.I’m trying to find things to enhance my web site!I suppose its ok to use a few of your ideas!!
http://bogin30.s3-website.ap-south-1.amazonaws.com/research/bogin30-(39).html
Dresses are made in gorgeous colors such as gold, purple, and blue and mother of the bride clothes.
What’s up to all, how is everything, I think every one is
getting more from this website, and your views are pleasant in favor of
new users.
It’s remarkable in support of me to have a website, which is useful for my knowledge.
thanks admin
Link exchange is nothing else except it is just placing the other person’s website link on your page at appropriate place and other person will also do same for you.
Thanks in support of sharing such a nice idea, article is good, thats why i have
read it entirely
Thank you for the good writeup. It in fact was a amusement account it.
Look advanced to more added agreeable from you! By the way,
how can we communicate?
My blog: top ten poker sites (nsmro.allhow.com)
But a smiling visitant here to share the love (:
, btw great design.
Have a look at my website; poker site rankings (http://Gosudar.Com.ru/go.php?url=aHR0cHM6Ly8zM3Bva2VyLm5ldA)