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
5 Killer Quora Answers To Link Daftar Gotogel Link daftar Gotogel
Hi! Someone in my Facebook group shared this site
with us so I came to give it a look. I’m
definitely loving the information. I’m bookmarking and will
be tweeting this to my followers! Excellent blog and wonderful style and design.
Cenforce 100mg tablets for sale: order cenforce – Cenforce 100mg tablets for sale
https://kamagra.win/# cheap kamagra
Buy Tadalafil 20mg Generic Cialis without a doctor prescription Cialis without a doctor prescription
Levitra tablet price: Levitra 20mg price – Levitra online USA fast
https://cenforce.pro/# Purchase Cenforce Online
You’ll Never Guess This Adhd In Adults Treatment’s Tricks Adhd In Adults Treatment
Is There A Place To Research Slot Reviews Online hacksaw gaming Slot Games
What Freud Can Teach Us About Combined Washer Dryer combined washer dryer
You suggested that really well!
15 Best Pinterest Boards Of All Time About Mazda Replacement Key mazda 2 key
http://cenforce.pro/# order cenforce
Window Companies Leeds Tips That Will Change Your Life leeds glazing (Francis)
https://kamagra.win/# cheap kamagra
10 Wrong Answers To Common Slot Demo Questions Do You Know The Right Ones?
slot demo pragmatic play
How Double Glazed Windows Repairs Its Rise To The No.
1 Trend In Social Media double glazed window repairs near me (Amanda)
The 12 Types Of Twitter Double Glazing Fitters Near Me
Accounts You Follow On Twitter Double Glazing fitters
Do you mind if I quote a couple of your posts as long as I provide credit
and sources back to your webpage? My blog is in the
very same niche as yours and my users would certainly benefit from a lot of the information you present here.
Please let me know if this ok with you.
Thank you!
I know this website gives quality dependent articles or
reviews and other data, is there any other web site which presents these kinds of data in quality?
https://viagras.online/# cheapest viagra
valtrex for sale cheap
https://levitrav.store/# buy Levitra over the counter
9kg Washing Machine Isn’t As Tough As You Think 9Kg Washing Machines
The 12 Most Popular Volvo Key Replacement
Cost Uk Accounts To Follow On Twitter volvo Key programming – extension.unimagdalena.edu.co,
Buy Cialis online Cialis 20mg price in USA Cialis without a doctor prescription
Evening Work From Home Jobs Tips From The Most Successful In The Business
Work From home jobs online
The Guide To Malpractice Lawyer In 2023 cleveland Heights malpractice Lawsuit
Why Is Online Shopping Websites List So Popular? Mizuno Black-Red Fastpitch Pants
Hand Bags Tommy Hilfiger Tools To Streamline Your Everyday Lifethe Only Hand Bags Tommy Hilfiger Technique Every Person Needs To Know Hand bags Tommy Hilfiger