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
Good day I am so happy I found your weblog, I really found you by accident,
while I was browsing on Digg for something else, Regardless
I am here now and would just like to say thanks for a tremendous post and a all round enjoyable
blog (I also love the theme/design), I don’t have time to go through it all at the moment but I have book-marked it and
also added in your RSS feeds, so when I have time I will be back to read a
lot more, Please do keep up the excellent job.
where buy clomid pills: clomid purchase online rex pharm – can you get generic clomid
Pills prescribing information. What side effects?
what is half life of buspirone
Actual news about medicament. Get information now.
Полезные советы по покупке диплома о высшем образовании без риска
Just want to say your article is as surprising. The clarity
in your post is simply nice and i can assume you’re an expert on this
subject. Well with your permission let me to grab your RSS feed to keep updated with forthcoming post.
Thanks a million and please carry on the rewarding work.
Сколько стоит диплом высшего и среднего образования и как его получить?
the dark internet blackweb official website https://darknetmarketstore.com/ – darkmarket list
Приобретение школьного аттестата с официальным упрощенным обучением в Москве
Как безопасно купить диплом колледжа или ПТУ в России, что важно знать
Официальная покупка школьного аттестата с упрощенным обучением в Москве
Drugs prescribing information. Long-Term Effects.
levonorgestrel brand name
Best what you want to know about medication. Read now.
buy dapoxetine online: cheap priligy – priligy maxpharm
amoxicillin over counter: amoxil – cost of amoxicillin 30 capsules
Вопросы и ответы: можно ли быстро купить диплом старого образца?
cost of cheap clomid without a prescription: clomid rex pharm – cheap clomid
BBgate MarketPlace 2024 Breaking Bad Gate Forum
BBgate MarketPlace
where to buy cheap clomid tablets: buy clomid – order cheap clomid without prescription
where to get amoxicillin over the counter: Amoxicillin buy online – where can i get amoxicillin
prednisone 10mg canada: generic Prednisone – prednisone without prescription medication
This article will help the internet users for creating new webpage or even a
weblog from start to end.
Пошаговая инструкция по безопасной покупке диплома о высшем образовании
Thanks a lot for sharing this with all people you really understand what you are talking approximately!
Bookmarked. Kindly also discuss with my site =). We may have a link
alternate agreement among us
get clomid tablets: clomid online – where buy generic clomid online
Официальная покупка диплома вуза с сокращенной программой обучения в Москве
This is a great tip particularly to those fresh to the blogosphere.
Simple but very precise info… Thanks for sharing this one.
A must read article!
where to get generic clomid price: clomid online – where buy generic clomid price
Где и как купить диплом о высшем образовании без лишних рисков
Аттестат школы купить официально с упрощенным обучением в Москве
Вопросы и ответы: можно ли быстро купить диплом старого образца?
There are various tools and websites that claim to allow users
to how do you view private instagram accounts private Instagram profiles, but it’s important to retrieve these later than caution. Many
of these tools can be unreliable, may require personal information, or could violate
Instagram’s terms of service. Additionally, using such tools can compromise your own security or lead to scams.
The safest and most ethical exaggeration to view
a private profile is to send a follow request directly to the user.
Always prioritize privacy and exaltation in your online interactions.