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
The Basics Of A Bridging Loan 다바오 밤문화
I don’t even know the way I finished up right here,
however I believed this submit was great. I don’t recognise who you might
be however definitely you are going to a famous blogger if you aren’t
already. Cheers!
лечение наркозависимости в стационаре https://www.ideya.forums.party/viewtopic.php?id=661 .
Вопросы и ответы: можно ли быстро купить диплом старого образца?
Диплом техникума купить официально с упрощенным обучением в Москве
I just had my roof replaced, and this article was super helpful in understanding the process! For those considering a replacement, check out Roofer Near Me for great resources
Официальное получение диплома техникума с упрощенным обучением в Москве
Hey There. I found your blog using msn. This is an extremely well written article.
I will make sure to bookmark it and come back to
read more of your useful information. Thanks for the post.
I’ll definitely return.
Официальная покупка диплома вуза с сокращенной программой обучения в Москве
Удивительно, но купить диплом кандидата наук оказалось не так сложно
вывод из запоя в стационаре воронежа zal.rolevaya.info/viewtopic.php?id=5384 .
Guide To Gspot Rabbit Toy: The Intermediate Guide Towards Gspot Rabbit Toy
gspot Rabbit toy
I’m no longer positive where you’re getting your information, but good topic.
I must spend some time finding out more or working out more.
Thank you for fantastic info I used to be looking for this information for my mission.
how to get on dark web tor markets links the dark internet
I wanted to thank you for this excellent read!! I absolutely loved every little
bit of it. I’ve got you book marked to check out
new things you post…
Also visit my site … Realtor in Meridian Mississippi
вывод из запоя в стационаре анонимно http://www.mozaisk.anihub.me/viewtopic.php?id=4371/ .
Drug prescribing information. Cautions.
where to buy levitra without insurance
Actual what you want to know about medicament. Get now.
Стоимость дипломов высшего и среднего образования и процесс их получения
Официальная покупка диплома вуза с сокращенной программой обучения в Москве
Полезные советы по безопасной покупке диплома о высшем образовании
Как официально купить диплом вуза с упрощенным обучением в Москве
вывод из запоя в стационаре вывод из запоя в стационаре .
Why visitors still use to read news papers when in this technological globe all
is presented on net?
Процесс получения диплома стоматолога: реально ли это сделать быстро?
Terrific article! This is the type of information that should be shared around the
internet. Disgrace on Google for now not positioning this submit higher!
Come on over and discuss with my web site .
Thank you =)
My webpage :: the growth matrix program free download
I know this if off topic but I’m looking into starting my
own blog and was curious what all is needed to get set
up? I’m assuming having a blog like yours would cost a pretty penny?
I’m not very internet savvy so I’m not 100% positive. Any suggestions
or advice would be greatly appreciated. Cheers
Feel free to visit my website – Live Casino House
The other day, while I was at work, my sister stole my apple ipad
and tested to see if it can survive a forty foot drop, just so she can be a youtube sensation. My iPad is now destroyed and she has 83 views.
I know this is entirely off topic but I had to
share it with someone!
Take a look at my web page: Russian shop
Официальная покупка аттестата о среднем образовании в Москве и других городах
Как получить диплом техникума официально и без лишних проблем
Как официально приобрести аттестат 11 класса с минимальными затратами времени