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
Приветствую. Подскажите, где найтиразные статьи о недвижимости? Пока нашел – https://teplodom78.ru
clopidogrel bisulfate 75 mg: cheapest plavix – Cost of Plavix on Medicare
It’s very effortless to find out any topic on net as compared to books, as I
found this post at this web page.
Приветствую. Может кто знает, где найтиполезные статьи о недвижимости? Пока нашел – https://teplodom78.ru
вывод из запоя в стационаре анонимно http://gov.ukrbb.net/viewtopic.php?f=3&t=6377/ .
Fachai Casino – best slot online in the Philippines
better than jili slot and pg slot
free Spins 3000+ Free Demo Slot
searching fachaicasino.org on Google or Bing to get free coins for play
https://fachaicasino.org/
all the time i used to read smaller articles that as well clear their motive, and that is also happening with this
post which I am reading at this time.
Hey! Do you know if they make any plugins to safeguard against
hackers? I’m kinda paranoid about losing everything I’ve worked
hard on. Any recommendations?
Impressive, what an insightful post! I really delighted in reading
about your outlook on this topic . It’s absolutely given me
a wealth to ponder .
I’d love to hear more of your thoughts , if you’re agreeable
to extending the dialogue .
Additionally , have you discovered of MEGA888?
It’s an wonderful virtual gaming platform with heaps of enthralling options
. I’ve participated in there and the experience has been exceptional.
Assuming that you’re in search of a novel way to
experience some fun and potentially triumph , I’d
emphatically recommend investigating it further.
Let me know if you’re interested and I shall provide more details !
Have a look at my site; mega888 singapore (https://canvas.instructure.com/)
Всем привет! Может кто знает, где найтиразные статьи о недвижимости? Пока нашел – https://teplodom78.ru
you are actually a excellent webmaster. The website loading speed is incredible.
It kind of feels that you’re doing any distinctive trick.
In addition, The contents are masterwork. you’ve done a fantastic activity
in this topic!
Приветствую. Может кто знает, где почитатьполезные статьи о недвижимости? Сейчас читаю – https://teplodom78.ru
This is really interesting, You’re a very skilled
blogger. I have joined your feed and look forward
to seeking more of your wonderful post. Also, I have shared your web site in my social networks!
Also visit my web-site … เว็บสล็อตแท้
prednisone daily use: generic Prednisone – prednisone 20 mg pill
I read this paragraph completely concerning the difference of hottest and earlier technologies, it’s awesome article.
deep web drug store deep web drug url dark web sites
ivermectin oral solution: generic Stromectol – minocycline ointment
https://plavixclo.com/# buy Clopidogrel over the counter
What’s up to every body, it’s my first pay a quick visit of this web site; this web site carries remarkable and actually
fine data in favor of visitors.
http://plavixclo.com/# Plavix 75 mg price
Misoprostol 200 mg buy online: cytpremium – Misoprostol 200 mg buy online
Adult Entertainment 오피사이트
Быстрая схема покупки диплома старого образца: что важно знать?
generic zithromax prices
buy minocycline 50mg otc stromectol nz stromectol ebay
Hey there! I just would like to offer you a big thumbs up for
your excellent information you’ve got right
here on this post. I’ll be coming back to your web site for more soon.
Покупка школьного аттестата с упрощенной программой: что важно знать
Your way of telling all in this piece of writing is actually
fastidious, all be capable of without difficulty know it, Thanks a lot.
п»їcytotec pills online cheapest cytotec Misoprostol 200 mg buy online
I don’t even know how I ended up here, but I thought this post was good.
I do not know who you are but certainly you are going to a famous blogger if you aren’t already 😉 Cheers!