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
Buy Cenforce 100mg Online: cenforce.pro – Purchase Cenforce Online
Great goods from you explainer video company india, man. I’ve understand your stuff explainer video previous to and you are just too fantastic. I actually like explainer video what you’ve acquired here, really like what you’re stating and the way in which you say it. You make it enjoyable and you still take care of to keep it smart. I can’t wait to read far more from you. This is really a terrific site. https://noida.yalwa.in/ID_141987385/Explainer-Video-Company-India.html
buy cenforce: Buy Cenforce 100mg Online – cenforce for sale
A Brief History History Of Welcome Kits avon start smart bag 2023 (coub.com)
Hi there friends, pleasant paragraph and good urging commented at this place, I am truly enjoying by these.
Levitra tablet price: Buy generic Levitra online – Cheap Levitra online
What’s The Fuss About Wall Mounted Fireplaces? Fireplaces And Stove
20 Reasons To Believe 10k Washing Machine Will Not Be Forgotten new 10Kg Washing Machine
Excellent post. I was checking constantly this explainer video blog and I am impressed! Very useful information specifically the last part 🙂 I care for such information a lot. I was looking for this particular information for a long time. Thank you and good luck. https://www.Gunspace.Net/explainer-video-company-india
15 Best 12kg Washing Machine Benefits You Should All Know 12kg Washing machine
http://kamagra.win/# Kamagra 100mg price
Upvc Windows Near Me Techniques To Simplify Your Everyday Lifethe Only Upvc Windows
Near Me Trick Every Individual Should Know Upvc windows near me
5 Killer Quora Answers On Boat Accident Attorneys boat accident attorneys
How Online Shopping Website In London Transformed My Life
For The Better Abdominal Workout Door Bar
Great post. I was checking continuously this explainer video blog and I am impressed! Very useful information specifically the last part 🙂 I care for such information a lot. I was seeking this particular info for a very long time. Thank you and best of luck. https://99designs.com.sg/profiles/8076816/about
What’s The Current Job Market For Double Glazed Window Repairs Professionals?
Double Glazed window Repairs
The Most Hilarious Complaints We’ve Been Hearing About
Double Glazed Window Suppliers Near Me double Glazing quotes
Are You Responsible For The Volvo Key Fobs Budget?
10 Very Bad Ways To Invest Your Money replacement Key for Volvo V50
How Do You Explain Who Owns CSGO To A Five-Year-Old Case esports
A Look Into The Future How Will The Washer Dryer Combos Industry Look Like In 10 Years?
machines
http://cialist.pro/# Tadalafil price
cenforce for sale buy cenforce buy cenforce
15 Twitter Accounts That Are The Best To Learn More
About Key Fob Programming Near Me reprogramming
15 Of The Best Twitter Accounts To Learn About Togel must a nice
cheapest cialis: buy cialis online – Generic Cialis price
How To Make A Profitable Seat Ibiza Key Fob Replacement When You’re Not
Business-Savvy seat leon car key replacement (qooh.me)
Cenforce 100mg tablets for sale cenforce for sale order cenforce
Very nice post . I just stumbled upon your blog and wanted to say that I have truly enjoyed browsing your blog posts. In any case I?ll be subscribing to your feed and I hope you write again soon! https://ru.stackoverflow.com/users/591467/explainer-video-company-india?tab=profile
One Key Trick Everybody Should Know The
One Work From Home Jobs No Experience Uk Trick Every Person Should Learn working remotely from home
http://kamagra.win/# Kamagra 100mg price