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
10 Mistaken Answers To Common Online Shopping Uk For Clothes Questions Do You Know The Correct Answers?
21-Inch Snow Blower
купить диплом в уфе купить диплом в уфе .
hey there and thank you for your info – I’ve certainly picked up anything new from right
here. I did however expertise several technical issues using this site,
since I experienced to reload the website many times previous to
I could get it to load correctly. I had been wondering if your
web host is OK? Not that I’m complaining, but sluggish loading instances times
will often affect your placement in google and could damage your high-quality score if ads and marketing with Adwords.
Anyway I’m adding this RSS to my e-mail and can look out for a lot more of your respective intriguing content.
Ensure that you update this again very soon.
5 Things That Everyone Doesn’t Know Concerning Auto Accident Law
Auto Accident Lawsuit
The Infrequently Known Benefits To Repair Upvc Windows upvc windows repairs near me (Rita)
10 Things We Hate About Jaguar Xf Key jaguar smart Key
15 Amazing Facts About Starlight Princess
Demo The Words You’ve Never Learned slot starlight Princess demo
The 3 Most Significant Disasters In Bonus Slots The Bonus Slots’s 3 Biggest Disasters In History Slots with bonuses
15 Things You’ve Never Known About Amazon Online Grocery Shopping Uk Weldon Shank Annular Cutter
CBD products have been gaining popularity in recent years for their potential health benefits. https://x-maxxcbdgummies.com is a brand that offers a range of CBD products, including gummies.
купить диплом колледжа http://www.ukr-diplom.ru .
купить диплом врача http://www.ukr-diplom.ru/ .
Thanks for sharing your thoughts about slot
depo. Regards
купить аттестат школы купить аттестат школы .
купить диплом инженера купить диплом инженера .
купить диплом технолога купить диплом технолога .
Medicament information for patients. Drug Class.
how to get minocycline price
Actual information about drugs. Get information now.
Very nice post. I simply stumbled upon your weblog and
wanted to say that I’ve truly loved surfing around your weblog posts.
After all I’ll be subscribing in your feed and I hope you write
again very soon!
Five Things You’ve Never Learned About Auto Accident Lawyers auto accident lawsuits
I’m gone to inform my little brother, that he should also go to see this webpage on regular basis to take updated from newest news update.
Incredible lots of very good facts!
Feel free to visit my blog post https://my-pet-extra.store/
Test: How Much Do You Know About Winning Slots? real money slots (bbs.theviko.com)
The 10 Scariest Things About Double Glazing Near Me
Double glazing Near me (wood21.co.Kr)
Having read this I believed it was really informative.
I appreciate you taking the time and energy to put this
content together. I once again find myself spending a lot of time both reading and leaving comments.
But so what, it was still worthwhile!
One Of The Most Innovative Things That Are Happening With
Sweet Bonanza Demo Free Spin Sweet Bonanza Free
Hello, i believe that i saw you visited my blog so
i got here to return the favor?.I am attempting to
to find things to enhance my site!I suppose its adequate
to make use of some of your concepts!!
купить дипломы о высшем образовании цена купить дипломы о высшем образовании цена .
10 Facts About Slot Variance That Can Instantly Put You In Good
Mood low variance slots
Seven Reasons To Explain Why Treadmill Is So Important treadmill For home use
What’s The Job Market For Window Repairs Leeds Professionals?
window repairs leeds – vwhiteskin.co.Kr,