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
Thank you for losing light on the value of specialist locksmiths! Great paintings! locksmith la porte tx
Hi just wanted to give you a brief heads up and let you know
a few of the pictures aren’t loading correctly.
I’m not sure why but I think its a linking issue. I’ve tried it in two different web
browsers and both show the same results.
http://clomidonpharm.com/# where can i get cheap clomid price
This design is spectacular! You most certainly know how to
keep a reader entertained. 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!
Wow, marvelous weblog layout! How long have you ever been blogging for?
you make blogging glance easy. The overall look of your web site is excellent, as neatly as the content!
where to get cheap clomid: buy generic clomid without prescription – order cheap clomid now
Тут можно преобрести купить взломостойкие сейфы сейф взломостойкий
Jugabet juegos Jugabet juegos .
buy prednisone online paypal: cheap prednisone online – prednisone 2 mg
Excellent pieces. Keep writing such kind of information on your page.
Im really impressed by your blog.
Hey there, You have done a fantastic job. I’ll certainly digg
it and individually suggest to my friends. I am confident they’ll be benefited from this web site.
Thanks for sharing such a fastidious opinion, post is fastidious, thats why i
have read it completely
can i buy generic clomid without rx clomidonpharm cheap clomid for sale
tor markets links dark web websites darknet links
Appreciate the thorough analysis. For more, visit website design
Как безопасно купить диплом колледжа или ПТУ в России, что важно знать
amoxicillin 500 mg online: where to buy amoxicillin – amoxicillin capsule 500mg price
can you buy amoxicillin uk amoxicillin online purchase amoxicillin 500 mg capsule
Как не попасть впросак при покупке диплома колледжа или ПТУ в России
Ꭲhhis isѕ tthe perfecht ƅoog forr aybody wwho rdalⅼy wantss tto finjd oout aboput this topic.
Yoou undetstand soօ muᥙch itss amost touhh too
arfue with yoou (not thyat I persxonally wull need to…HaHa).Youu certainly puut a brand nnеw
spoin onn a toppic hat hass bbeen wrotten aout ffor ages.
Great stuff, jjust excellent!
Feell feee tto sսrff tto mmy weebpage :: ورود به بهترین سایت
When someone writes an article he/she maintains the idea
of a user in his/her mind that how a user can be aware of it.
Therefore that’s why this piece of writing is great. Thanks!
Hi there! I know this is kinda off topic but I was wondering which blog platform are
you using for this website? I’m getting sick
and tired of WordPress because I’ve had issues with hackers and I’m looking at alternatives for another
platform. I would be fantastic if you could point me
in the direction of a good platform. https://Palmer-Electrical.com/question/comptoir-de-cuisine-en-stratifie-un-choix-pratique-et-economique-2/
my review here https://mangoandmoreshop.com/2024/06/13/77bet-slot-petualangan-seru-di-dunia-permainan-online/
get generic clomid without rx clomidonpharm where to buy generic clomid without a prescription
Jugabet promociones Jugabet promociones .
Wow, this piece of writing is fastidious, my younger sister is analyzing these kinds of things,
therefore I am going to let know her.
pinco зеркало
buy amoxicillin where can i buy amoxocillin buy amoxicillin 500mg
Thank you, I’ve recently been searching for
information approximately this topic for a while and yours is the best I’ve discovered so far.
However, what about the bottom line? Are you certain in regards to
the source?
Why Nobody Cares About Specializes In Asbestos Litigation Asbestos lawyers
That is a great tip especially to those new to the blogosphere.
Brief but very accurate information… Many thanks for sharing
this one. A must read post!