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
ultrabet guncel: ultrabet guncel – ultrabet giris
Диплом техникума купить официально с упрощенным обучением в Москве
Some Of The Most Ingenious Things That Are Happening With Asbestos Attorney In Virginia Mesothelioma Attorneys
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 web site.
Studying this information So i am glad to convey that I’ve a very excellent uncanny feeling
I came upon just what I needed. I so much indisputably will make certain to
do not omit this website and give it a glance regularly.
my site :: zapada01
Howdy very nice site!! Man .. Excellent .. Superb ..
I’ll bookmark your site and take the feeds also?
I’m happy to search out a lot of helpful information here in the post,
we want develop more strategies on this regard,
thanks for sharing. . . . . .
What Is The Reason Best Pod Coffee Machine Is Right For
You best value coffee machine; jisuzm.com,
Приобретение диплома ПТУ с сокращенной программой обучения в Москве
Hey I know this is off topic but I was wondering if you knew of
any widgets I could add to my blog that automatically tweet my newest twitter updates.
I’ve been looking for a plug-in like this for quite some time and was hoping maybe
you would have some experience with something like this.
Please let me know if you run into anything. I truly enjoy
reading your blog and I look forward to your new updates.
deneme bonusu veren siteler mycbet.com deneme bonusu veren siteler yerliarama.org deneme bonusu veren yeni siteler
Тут можно преобрести сейф для оружия сейфы оружейные купить
en kazancl? slot oyunlar? az parayla cok kazandiran slot oyunlar? en kazancl? slot oyunlar?
Just want to say your article is as astonishing. The clearness for your put up is simply
great and that i could suppose you’re knowledgeable on this subject.
Fine with your permission let me to grasp your RSS
feed to stay up to date with drawing close post.
Thank you one million and please continue the gratifying work.
What i do not understood is in reality how you are now not actually
a lot more smartly-favored than you might be right now. You are
so intelligent. You realize therefore significantly in relation to this matter, produced me personally consider it from a lot of various angles.
Its like men and women aren’t involved except it is something to do with Girl gaga!
Your own stuffs excellent. Always deal with it up!
matadorbet matadorbet.bid matadorbet.bid
15 Trends That Are Coming Up About Wall Mounted Electric Fireplaces white wall mounted electric Fireplaces
matadorbet.bid: matadorbet giris – matadorbet.bid
What’s up to all, how is the whole thing, I think every
one is getting more from this site, and your views are fastidious for new viewers.
Узнайте стоимость диплома высшего и среднего образования и процесс получения
Hi my family member! I wish to say that this article is amazing, great written and include almost all vital infos.
I’d like to see more posts like this .
5 Killer Quora Answers On Cabin Bed Mid Sleeper With
Storage cabin Bed mid Sleeper
http://casinositeleri.win/# Casino Siteleri
deneme bonusu veren siteler denemebonusu2026.com
Всё, что нужно знать о покупке аттестата о среднем образовании
11 “Faux Pas” That Are Actually OK To Create With Your Wheelchair Folding Ramp Van Ramps For Wheelchairs – Mobilemobilityscooters87210.Aioblogs.Com,
I have learn some excellent stuff here. Definitely price bookmarking for revisiting.
I wonder how a lot attempt you put to make the sort of fantastic
informative site.
Тут можно преобрести сейф для оружия цена сейф купить оружейный
slot siteleri: en cok kazand?ran slot oyunlar? – en cok kazand?ran slot oyunlar?
Virtual Mystery Boxes Isn’t As Difficult As You Think Mystery Box opening
I blog often and I truly appreciate your content. This article has truly peaked
my interest. I am going to book mark your blog and keep checking for new information about once
a week. I subscribed to your Feed as well.
Тут можно преобрести сейфы для ружья купить шкаф для ружья
Howdy would you mind stating which blog platform you’re using?
I’m planning to start my own blog in the near future but I’m
having a hard time selecting between BlogEngine/Wordpress/B2evolution and Drupal.
The reason I ask is because your design and style seems different then most blogs and I’m looking for something unique.
P.S My apologies for getting off-topic but I had to ask!