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
priligy: priligy maxpharm – cheap priligy
Oh my goodness! Incredible article dude! Thanks, However I am
going through troubles with your RSS. I don’t know why
I am unable to subscribe to it. Is there anybody else
getting similar RSS problems? Anyone that knows
the answer will you kindly respond? Thanks!!
This article presents clear idea designed for the new users of blogging, that really how
to do blogging.
Как получить диплом техникума с упрощенным обучением в Москве официально
Парадокс, но купить диплом кандидата наук оказалось не так и сложно
dark market list dark market list blackweb
max pharm: priligy max pharm – priligy maxpharm
can i purchase cheap clomid without insurance: clomid – how to buy cheap clomid prices
Как приобрести аттестат о среднем образовании в Москве и других городах
Hi friends, good paragraph and pleasant arguments commented at this place, I am really enjoying
by these.
Узнай все о варикоцеле симптомы варикоцеле у мужчин симптомы
Hello there, I found your website by way of Google whilst looking for a similar subject, your
site came up, it seems to be great. I’ve bookmarked it in my google bookmarks.
Hello there, simply was aware of your weblog through Google, and
located that it is truly informative. I am going to watch out for brussels.
I’ll appreciate if you happen to continue this in future.
Numerous other folks will be benefited out of your writing.
Cheers!
Узнай все о чем опасно варикоцеле лапароскопия варикоцеле
If you wish for to grow your knowledge just keep visiting this website and be updated with the latest news posted here.
prednisone 20 mg generic: prednisoneraypharm – buy prednisone canadian pharmacy
Всё, что нужно знать о покупке аттестата о среднем образовании без рисков
Покупка диплома о среднем полном образовании: как избежать мошенничества?
cost clomid without a prescription: clomid rex pharm – where to get generic clomid online
where can i buy generic clomid pills: clomid – how to buy generic clomid no prescription
Приветствую. Подскажите, где почитатьразные блоги о недвижимости? Пока нашел – https://credomir.ru
Всем привет! Может кто знает, где найтиполезные статьи о недвижимости? Сейчас читаю – https://credomir.ru
how can i get clomid prices: clomid rex pharm – where to buy cheap clomid pill
Great article! That is the kind of info that should be shared
across the web. Shame on the search engines for no longer positioning this publish higher!
Come on over and seek advice from my website . Thanks =)
fexofenadine 180mg canada
I all the time emailed this webpage post page to all my associates, as if like to read
it afterward my contacts will too.
Всем привет! Может кто знает, где найтиполезные блоги о недвижимости? Сейчас читаю – https://credomir.ru
Всем привет! Может кто знает, где почитатьразные блоги о недвижимости? Сейчас читаю – https://credomir.ru
Its like you read my thoughts! You seem to grasp so much approximately this, like you wrote the book in it or something.
I feel that you just could do with a few % to pressure the message house a
bit, however instead of that, that is excellent blog. An excellent read.
I’ll definitely be back.
hello there and thank you for your info – I’ve certainly picked up anything new from right here.
I did however expertise a few technical points using this website, since I experienced to reload the web site a lot
of times previous to I could get it to load correctly.
I had been wondering if your web hosting is OK? Not that I’m complaining,
but sluggish loading instances times will sometimes affect
your placement in google and can damage your quality score if advertising and marketing with
Adwords. Well I’m adding this RSS to my e-mail and can look out for much more of your
respective exciting content. Ensure that you update this again soon.
Пошаговая инструкция по официальной покупке диплома о высшем образовании
blogsfere.com/viewtopic.php?t=203891