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
15 Terms Everyone Working In The Bed And Couch Industry Should Know deep
seat couch (Ollie)
https://pharm24.pro/# ed remedies that really work
prescription meds without the prescriptions
indian pharmacy online Best online Indian pharmacy cheapest online pharmacy india
I enjoy reading a post that can make men and women think.
Also, many thanks for permitting me to comment!
Hi, i read your blog occasionally and i own a similar one and i was just curious if you get a
lot of spam remarks? If so how do you reduce it, any plugin or anything you can advise?
I get so much lately it’s driving me insane so any support is
very much appreciated.
Официальная покупка диплома ПТУ с упрощенной программой обучения
20 Trailblazers Setting The Standard In Best Car Accident Lawyer Near Me Car accident and injury lawyers
1 win bet
can i purchase generic doxycycline for sale
не приходит смс с кодом Телеграм
5 Killer Quora Answers On Automatic Folding Electric Scooter automatic
folding electric scooter (Trevor)
10 Mobile Apps That Are The Best For French Door Fridge On Sale french door fridge without ice maker
1win aviator
This post is priceless. When can I find out more?
https://databullseye2.ambarita.org/
20 Things You Should Be Educated About Best Car Accident Lawyers attorney for Car accident Injury
Чтобы получить презент, необходимо внести на баланс минимум 500
рублей.
10 Real Reasons People Dislike Pragmatic Authenticity Verification Pragmatic Authenticity Verification 프라그마틱 정품 확인법
Fridge Freezer Cheap Tools To Improve Your Daily Life Fridge Freezer Cheap Trick That Everyone Should Be Able To fridge Freezer cheap
Successful treatment is available when you Viagra Oral Jelly online you should consult your physician.
Are You Responsible For An Double Double Bunk Beds Budget?
10 Terrible Ways To Spend Your Money double bunk bed cheap
It’s very straightforward to find out any matter on net as compared to
books, as I found this article at this web site.
7 Things About Sports Toto Official Website You’ll Kick Yourself
For Not Knowing 토토사이트 순위 (Sportstoto50387.buscawiki.Com)
купить официально диплом о высшем prema-diploms.ru .
top 10 pharmacies in india: indian pharm 24 – world pharmacy india
Guide To Electric Patio Heaters Uk: The Intermediate Guide
In Electric Patio Heaters Uk Electric patio heaters UK
Покупка школьного аттестата с упрощенной программой: что важно знать
Why People Don’t Care About Car Accidents Lawyers Near Me Top Rated Car Accident Attorney
Wow, amazing blog layout! How long have you been blogging for?
you make blogging look easy. The overall look of your web site is excellent, let alone
the content!
So , You’ve Purchased Replacement Key For Audi A3 …
Now What? audi car key replacement cost (https://auto-locksmiths10548.dreamyblogs.com)
top 10 pharmacies in india: Indian pharmacy international shipping – indian pharmacy