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
Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something.
I think that you could do with a few pics to drive the message home a bit,
but other than that, this is great blog. A great read. I’ll certainly be back.
You actually expressed it superbly.
buy ozempic pills online ozempic generic buy ozempic
Sweet blog! I found it while searching on Yahoo News.
Do you have any tips on how to get listed in Yahoo News?
I’ve been trying for a while but I never seem to
get there! Thanks
Наш Психолог сейчас
10 Things We All Are Hateful About Double Glazing Window Handle aluminium window Handle
7 Helpful Tips To Make The Most Out Of Your Adult Toys Store best Adult sex toys for Men
Guide To Integrated Fridge Freezer 70 30: The Intermediate Guide On Integrated Fridge Freezer 70 30 integrated Fridge freezer 70 30
buy cheap ozempic ozempic ozempic online
Hi there just wanted to give you a brief heads up and let you know a few of
the images aren’t loading correctly. I’m not sure why but I think its a
linking issue. I’ve tried it in two different browsers and both show the same outcome.
Thanks for the auspicious writeup. It actually was once a leisure account it.
Look complex to far added agreeable from you! However, how
can we keep in touch?
p2m8oo
https://rybelsus.shop/# rybelsus cost
Официальная покупка диплома ПТУ с упрощенной программой обучения
Hi there! This is kind of off topic but I need some help from an established blog.
Is it tough to set up your own blog? I’m not very techincal but
I can figure things out pretty quick. I’m thinking about making my own but I’m not sure where to begin. Do you have any ideas or suggestions?
With thanks
Does your blog have a contact page? I’m having a tough time locating it but, I’d like to send you an e-mail.
I’ve got some creative ideas for your blog you might
be interested in hearing. Either way, great site and I
look forward to seeing it grow over time.
buy semaglutide pills semaglutide cost buy semaglutide online
Useful info. Fortunate me I discovered your website accidentally,
and I am shocked why this accident didn’t came about in advance!
I bookmarked it.
This is my first time pay a visit at here and
i am in fact impressed to read everthing at one place.
Look at my website … https://www.cucumber7.com/
Hey There. I found your blog using msn. This is
a very well written article. I will make sure to bookmark it and return to
read more of your useful info. Thanks for the post.
I will certainly comeback.
15 Startling Facts About Krups Nespresso Machine You’ve Never Heard Of nepresso coffee machines
5 Common Myths About Boot Scooters You Should Stay Clear Of Boots Scooter
What i don’t realize is in reality how you’re no longer really much
more smartly-liked than you may be right now. You are very intelligent.
You recognize thus significantly in relation to this subject, produced me
individually imagine it from numerous numerous angles.
Its like men and women are not involved until it is
one thing to accomplish with Lady gaga! Your individual stuffs great.
At all times care for it up!
rybelsus coupon: rybelsus pill – rybelsus cost
https://rybelsus.shop/# semaglutide online
В магазине сейфов предлагают стоимость сейфа купить сейф оптом
lesbian porn
not safe
What Is Butt Plug Online Store? History Of Butt Plug Online Store In 10
Milestones butt Shaped sex toy
В магазине сейфов предлагают сейф купить сейф цена москва