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 been exploring for a bit for any high-quality articles or blog posts on this kind of house .
Exploring in Yahoo I finally stumbled upon this website.
Reading this info So i’m happy to exhibit that I have a very good uncanny feeling I
came upon just what I needed. I most surely will make sure to do not
fail to remember this web site and give it a look regularly.
Узнай все о удаление полипов в матке в москвеудаление полипа во влагалище
Nicely spoken genuinely! !
http://www.mptextile.ru — Решения для создания функциональной и стильной кухни.
I have been browsing on-line more than three hours
these days, yet I never found any attention-grabbing article like yours.
It’s pretty price sufficient for me. Personally, if all web owners and bloggers made good content
material as you did, the internet can be a lot more helpful
than ever before.
Узнай все о гистероскопия удаление полипаудаление полипа эндометрия в матке
Узнай все о гистерорезектоскопия полипа маткиудалить полип на шейке матки
I blog frequently аnd I reɑlly appreciate your content.
Ꭲһis great article has truly peaked mmy іnterest. I will bok mark yoour site аnd keеp checking fоr new іnformation abоut once peer ԝeek.
I opted in for yoսr RSS feed aas well.
Ηere іs my website WengToto
deep web drug url blackweb official website https://darknetmarketstore.com/ – dark market onion
Good day I am so delighted I found your web site, I really found you by mistake, while I was searching on Google for something else, Anyhow I am here now and would just like to
say many thanks for a remarkable post and a all round entertaining blog (I also
love the theme/design), I don’t have time to go through it all at the minute but I have book-marked it and
also included your RSS feeds, so when I have time I will be back to read much more, Please do keep
up the great job.
What’s up i am kavin, its my first occasion to commenting anywhere, when i
read this article i thought i could also make comment due
to this sensible paragraph.
Узнай все о гистероскопия матки удалениеполип в матке операция
You are so cool! I do not suppose I have read through something like this before.
So great to discover another person with unique thoughts
on this subject matter. Really.. thank you for
starting this up. This website is one thing that is needed on the web, someone with some originality!
What’s up to every one, it’s truly a pleasant for me to go to see this web page, it includes
helpful Information.
Medicines information leaflet. Cautions.
how can i get rogaine without a prescription
Some information about drug. Read information now.
Pills information sheet. Brand names.
order cheap prozac for sale
All information about medicine. Get information now.
What’s Going down i am new to this, I stumbled upon this I have found It positively helpful and it has aided me out loads.
I hope to give a contribution & aid other users like its aided me.
Good job.
Value Your Input!
I’m Elated you Located the Post Helpful.
If you’re Enthusiastic about Venturing into more Prospects in the
online Sports Space, I’d Recommend Delving into CMD368.
They Offer a Diverse array of Captivating Wagering Choices, On-demand content, and a
Straightforward Interface.
What I Highly Enjoy about CMD368 is their Dedication to Controlled Bookmaking.
They have Effective Safety and Functionalities to
Help Users Manage their actions.
Irrespective of whether you’re a Seasoned Punter
or Unfamiliar to the Gambling, I Gather you’d Highly Relish the Engagement.
Please Create an account Through the URL and Get in touch if you have Further Uncertainties.
Also visit my website: sports betting (ralph.bakerlab.org)
Как приобрести диплом техникума с минимальными рисками
Your style is unique compared to other people I have read
stuff from. Many thanks for posting when you have the opportunity,
Guess I’ll just book mark this web site.
My family members all the time say that I am killing my time
here at net, however I know I am getting know-how all the time by reading thes fastidious posts.
I used to be recommended this blog via my cousin. I
am not certain whether this post is written via him as nobody
else know such specific approximately my difficulty.
You are amazing! Thanks!
Полезные советы по безопасной покупке диплома о высшем образовании
Официальная покупка диплома вуза с сокращенной программой обучения в Москве
Быстрая схема покупки диплома старого образца: что важно знать?
This means we might make a small commission at no further price to you should you make a purchase via one in every of our
hyperlinks. This post could comprise affiliate hyperlinks.
Selection could range between shops and online. So, it’s as simple as scrolling by out collection of cat coloring
pages and clicking on the download hyperlink beneath the cat coloring footage you want to obtain.
Coloring is not only for kids; it’s an effective way for adults to chill
out and express creativity. They supply a artistic outlet
and a singular solution to learn about totally different cat breeds and their behaviors.
In this enlightening e book, McNamee delves into the feline psyche, exploring the emotional,
social, and mental lives of cats and shedding mild on their mysterious behaviors.
And what could be better than coloring your
favourite feline pals? These free printable cat coloring sheets
will not be only a fun exercise; they serve as a terrific method to bond with
family and pals.
Today, while I was at work, my cousin stole my iphone and
tested to see if it can survive a 40 foot drop, just so she can be a youtube sensation. My iPad is now broken and she has 83 views.
I know this is entirely off topic but I had to share it
with someone!
Как правильно купить диплом колледжа и пту в России, подводные камни
hello there and thank you for your info –
I have certainly picked up something new from right here.
I did however expertise a few technical points using this site, as
I experienced to reload the website many times previous
to I could get it to load correctly. I had been wondering if your
web hosting is OK? Not that I am complaining, but slow loading instances times will sometimes
affect your placement in google and can damage your quality score if advertising and marketing with Adwords.
Anyway I’m adding this RSS to my email and could look out for
a lot more of your respective interesting
content. Make sure you update this again soon.
Fastidious response in return of this issue with firm arguments and telling all concerning that.