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
Your style is so unique in comparison to other folks I
have read stuff from. Thanks for posting when you have the
opportunity, Guess I’ll just book mark this page.
Excellent post! We are linking to this particularly great content on our site.
Keep up the good writing.
вавада казино: vavada-kazi.ru – вавада
Definitely believe that which you stated. Your favorite reason appeared to be on the net the easiest thing
to be aware of. I say to you, I definitely get annoyed
while people consider worries that they just do not know about.
You managed to hit the nail upon the top and defined out the whole thing without having side effect ,
people can take a signal. Will probably be back to get more.
Thanks
pinup kazi: пин ап казино официальный сайт – pinup-kazi.ru
Medication information sheet. Effects of Drug Abuse.
can you get cheap olmesartan price
Some news about medication. Get now.
Medication prescribing information. Long-Term Effects.
order cheap elavil pill
Actual what you want to know about medicament. Read information now.
вавада онлайн казино: вавада онлайн казино – вавада казино онлайн
vavada: вавада казино зеркало – вавада казино
pinup kazi: пин ап казино онлайн – пин ап казино
If you’re unsure about your personal injury case, consulting a professional is key! Visit personal injury attorney for more information
bitcoin dark web dark web links tor market
вавада казино: vavada-kazi.ru – вавада казино зеркало
The fact that most warranties last decades when choosing quality materials like steel is encouraging—what warranties have people found most helpful over time? Discuss here or read warranty comparisons on my site: roofers vancouver
Your mode of explaining everything in this piece of writing is in fact pleasant, every
one be capable of without difficulty be aware of it,
Thanks a lot.
вавада казино зеркало: vavada kazi – вавада онлайн казино
Fantastic blog! Do you have any tips for aspiring writers?
I’m planning to start my own website soon but I’m a little lost on everything.
Would you propose starting with a free platform like WordPress or
go for a paid option? There are so many choices out there that I’m completely
overwhelmed .. Any recommendations? Cheers!
Полезные советы по безопасной покупке диплома о высшем образовании
вавада казино: вавада – казино вавада
пин ап зеркало: пин ап вход – пинап казино
пин ап вход: pinup-kazi.ru – pinup
How long can mirtazapine side effects in dogs with ED treatments?
Hello everyone, it’s my first pay a quick visit at this web site, and piece of writing is in fact fruitful in favor of me, keep up posting such posts.
Telegram 是一款主打隐私保护和高安全性的即时通讯应用,广泛支持全球用户。虽然 Telegram 没有发布专门的“中文版”,但其支持简体中文和繁体中文语言,用户可以通过设置轻松切换到中文界面。Telegram 提供了包括加密聊天、语音通话、文件传输、大型群组和频道等丰富的功能,并且支持跨平台同步,用户可以在手机、电脑等设备上无缝使用。https://www.telegramhc.com
Быстрая схема покупки диплома старого образца: что важно знать?
вавада казино зеркало: казино вавада – казино вавада
darknet market deep dark web darknet drugs
I am not sure where you’re getting your info, but great topic.
I needs to spend some time learning more or understanding more.
Thanks for fantastic info I was looking for this information for my mission.
High Stakes Poker davao
Всё, что нужно знать о покупке аттестата о среднем образовании без рисков