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
Appreciating the time and effort you put into your blog and detailed information you present.
It’s great to come across a blog every once in a while that isn’t the same old rehashed material.
Wonderful read! I’ve bookmarked your site and I’m including
your RSS feeds to my Google account.
great points altogether, you simply won a new reader.
What could you suggest in regards to your submit that you just
made a few days ago? Any positive?
I really like reading through an article that can make
men and women think. Also, thank you for permitting me to comment!
Thanks for sharing such a nice opinion, paragraph is good,
thats why i have read it completely
Standup Comedy: Comedy Clubs Vs One Nighters 상무지구오피 (http://balloonmobile.com)
Thanks for your personal marvelous posting!
I certainly enjoyed reading it, you can be a
great author.I will be sure to bookmark your blog and may come back later on. I want to encourage
continue your great posts, have a nice holiday
weekend!
kök hücre saç ekimi
Thanks for sharing such a pleasant idea, post is good, thats why i have read it completely
I think the admin of this site is genuinely working hard for his website, since here every
material is quality based information.
Also visit my webpage :: testoprime reviews
I feel that is among the such a lot vital info for me.
And i am satisfied studying your article. However wanna remark on some common issues, The
website taste is ideal, the articles is in point of fact nice : D.
Excellent job, cheers
I just couldn’t leave your site before suggesting that I extremely enjoyed the usual info a person supply to your visitors?
Is going to be again often in order to inspect new posts
https://sweetbonanza.network/# sweet bonanza bahis
http://denemebonusuverensiteler.win/# bahis siteleri
https://vk.com/@vykupbityhavtomoskva-vykup-avto-mini-mini
You can definitely see your enthusiasm within the work you write.
The sector hopes for even more passionate writers such as you who aren’t afraid
to mention how they believe. At all times go after your heart.
Heya i am for the first time here. I found
this board and I find It truly useful & it
helped me out much. I hope to give something back and aid others
like you helped me.
watch porn video
Keep on writing, great job!
Hi there, You have done a great job. I’ll certainly digg it and
personally suggest to my friends. I am sure they will be benefited from
this web site.
Hi there, I enjoy reading all of your post. I wanted to write a little comment
to support you.
10 Mini Cooper Key Replacement Tricks Experts Recommend spare mini key (Gemma)
Hey there! This is my 1st comment here so I just wanted to give a quick shout
out and tell you I genuinely enjoy reading your posts.
Can you recommend any other blogs/websites/forums that go over
the same topics? Thanks!
5 Clarifications On Subaru Key Fob Subaru new Key
Everyone loves what you guys are up too. This type of clever work and exposure!
Keep up the terrific works guys I’ve incorporated you guys to
my own blogroll.
It’s very straightforward to find out any matter on net as compared to textbooks, as I found this post at this web site.
Nice blog here! Also your website loads up very fast! What host are you using?
Can I get your affiliate link to your host? I wish my
web site loaded up as quickly as yours lol
I love your blog.. very nice colors & theme.
Did you make this website yourself or did you hire someone to do it
for you? Plz answer back as I’m looking to design my own blog and would like to find out where
u got this from. thanks a lot
https://slotsiteleri.bid/# canl? slot siteleri
Hello! This post couldn’t be written any better! Reading through
this post reminds me of my old room mate! He always kept chatting about this.
I will forward this page to him. Fairly certain he will have a good read.
Thank you for sharing!
http://denemebonusuverensiteler.win/# deneme bonusu
slot siteleri 2024: en iyi slot siteler – slot siteleri 2024