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
XYGENYX, a licensing company for FDA-registered items.
my site :: Bookmarks
A synthetic USP human development hormone (somatropin).
my homepage :: Bookmarks
Thanks for every other magnificent article. The place else may just anybody get that type of
info in such a perfect method of writing? I’ve a presentation subsequent week,
and I’m on the look for such info.
Viagra pas cher livraison rapide france: Viagra prix – Viagra sans ordonnance 24h suisse
NewULife Ranks. Welcome to my NewUlife Testimonial.
Feel free to surf to my blog post: nib meaning in construction
USP human development hormone (somatropin).
Feel free to surf to my website; can a foundation apply for grants
Its History Of 3 Wheel Jogger Stroller lightweight 3 wheel buggy (Katherin)
http://pharmaciepascher.pro/# pharmacie en ligne
What is New U Life SOMADERM Transdermal Gel.
My website … nib foundation logo
Drag racing tshirt by dxterity styles on instagram.
my website – t shirt design website free
XYGENYX, a licensing firm for FDA-registered items.
Take a look at my blog post – is foundation necessary in makeup
What is New U Life SOMADERM Transdermal Gel.
my website replacement window cost average
There are several areas to find tee t shirt design app iphone designs.
An artificial USP human development hormone (somatropin).
Feel free to visit my web page; vigabatrin powder instructions
Let me show you in my review of NewULife.
my web blog: Can A Foundation Apply For Grants
https://vgrsansordonnance.com/# Viagra prix pharmacie paris
What is New U Life SOMADERM Transdermal Gel.
Feel free to visit my webpage – Bookmarks
충남 천안 성정동 남자라면 천안노래방에 한 번쯤 와보셔야죠
강서구 핫 플레이스는 어디?마곡 룸싸롱이죠!! 다들 연락 주세요.
pharmacie en ligne france livraison belgique: pharmacie en ligne pas cher – pharmacie en ligne france livraison internationale
In this situation, the life insurance policy company’s loan.
Take a look at my blog post … Bookmarks
I like the valuable info you provide in your articles.
I will bookmark your weblog and check again here frequently.
I am quite certain I’ll learn lots of new stuff right here!
Good luck for the next!
http://vgrsansordonnance.com/# Viagra sans ordonnance 24h Amazon
Wonderful blog! I found it while surfing around
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!
Appreciate it
My web-site has anyone used herpesyl
What is New U Life SOMADERM Transdermal Gel.
Also visit my website – Bookmarks
XYGENYX, a licensing company for FDA-registered items.
Feel free to visit my web site – Bookmarks
An artificial USP human development hormone (somatropin).
Have a look at my page :: window replacement near me car
XYGENYX, a licensing business for FDA-registered items.
Also visit my web blog :: vigabatrin brand name
Typeography TShirt Style Example Bootstrapping.
Also visit my website … t shirt design app
Remarkable things here. I am very glad to peer your
article. Thank you so much and I am having a look forward to
contact you. Will you kindly drop me a e-mail?