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 got this site from my friend who told me on the topic of this website and now this time I am browsing this site and
reading very informative articles or reviews here.
The United State Fda (FDA) has not authorized any of these
items.
быстрый вывод из запоя ростов быстрый вывод из запоя ростов .
срочный вывод из запоя ростов срочный вывод из запоя ростов .
Hmm it seems like your website ate my first comment (it was extremely long)
so I guess I’ll just sum it up what I had written and say, I’m thoroughly
enjoying your blog. I too am an aspiring blog blogger but I’m still new to everything.
Do you have any recommendations for newbie blog writers?
I’d definitely appreciate it.
If you’ve been involved in a car accident in Seattle, it’s crucial to seek help from a qualified chiropractor. They can provide the necessary care to address any pain or discomfort resulting from the incident Seattle Car Accident Chiropractor
This was beautifully organized. Discover more at ift physiotherapy
бонусы
вывод из запоя цена ростов вывод из запоя цена ростов .
вывод из запоя недорого ростов вывод из запоя недорого ростов .
I recently completed a kitchen remodel on Mercer Island, and the transformation is unbelievable! It’s amazing how a new layout and updated appliances can completely change the feel of a home Remodel Contractor
Well done! Find more at roof repair
buy viagra online
http://slotsiteleri25.com/# slot oyunlar? puf noktalar?
10 Back Injury Lawsuits Tricks All Experts Recommend Injury Lawyers
(Squareblogs.net)
Altid har et overblik over forbrug og fakturaer uden at gРµ pРµ kompromis. Uden Recept Online brGenerisk Alternativ Til Topamac Brug A Amexbr. Levitra cialis apotek Levitra bald billiger Para que sirve el levitra Cada, custa. Inderal Utan Recept, Apotek Online Sverige, liKolo Carts. Usa. Prices the sildenafil tablets buy bulk, Potenspiller uden recept, Nord Apotek. https://danmarkgenerisk.com/ancozan/ Kobe Ancozan Pille
вывод из запоя цена на дому вывод из запоя цена на дому .
https://casinositeleri25.com/# deneme bonusu veren casino siteleri
http://casinositeleri25.com/# Casino Siteleri
This design is steller! You obviously know how to keep a reader
amused. Between your wit and your videos, I
was almost moved to start my own blog (well, almost…HaHa!) Wonderful job.
I really enjoyed what you had to say, and more
than that, how you presented it. Too cool!
My page – temporary floor protection
Loved your pointers on stopping split ends; they’re awesome life like and trouble-free to put into effect! More care ideas may be came upon at hair salon in west vancouver
kurumsal web tasarım beylikdüzü
I am no longer positive where you’re getting your info, but great topic.
I needs to spend some time learning more or understanding
more. Thank you for excellent information I used to be in search of
this information for my mission.
http://sweetbonanza25.com/# sweet bonanza oyna
deneme bonusu veren siteler deneme bonusu veren yeni siteler deneme bonusu veren siteler yeni
The concept of multi-level decks was fascinating; it adds so much depth deck builder in charlotte
google sertifikalı kurumsal web tasarım
I all the time used to study paragraph in news papers
but now as I am a user of internet so from now I am using net for
posts, thanks to web.
If some one needs expert view on the topic of running a blog then i
advise him/her to pay a visit this web site, Keep up the nice work.
en iyi bahis uygulamasД± Casino Siteleri Deneme Bonusu Veren Siteler
Nunca pensei que a alimentação influenciasse tanto na saúde bucal. Vou ficar mais atento agora e procurar uma Facetas de Resina maristas para orientações
How To Outsmart Your Boss On Train Injury Lawsuit 18 Wheeler Truck Accident Attorney