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
Medicament information for patients. Effects of Drug Abuse.
can i order generic irbesartan
All information about medicament. Get information now.
You’ll Be Unable To Guess Online Shopping Sites Top 7’s Tricks Online Shopping Sites Top 7
לחצים ודאגות. ולכן עכשיו זה הזמן לעצור ולשים את כל בצד. ואין דרך טובה יותר לעצור את המחשבות והדאגות, מאשר בילוי מענג שכולו התשובה היא פשוטה, עיסוי אירוטי בחולון הוא הפינוק המושלם לגבר. זהו הפינוק שכל גבר צריך וכל גבר רוצה. גברים רבים נהנים מעיסויים נערות ליווי מלאות
What’s The Job Market For Replacement Windows Prices Professionals?
Replacement Windows Prices
I loved as much as you’ll receive carried out right here.
The sketch is tasteful, your authored subject matter stylish.
nonetheless, you command get got an edginess over that you wish be delivering
the following. unwell unquestionably come further formerly again as exactly the same nearly a
lot often inside case you shield this increase.
Some Of The Most Ingenious Things That Are Happening With Upvc Windows Repair repair Upvc Window
Why Everyone Is Talking About Online Shopping Uk Electronics This Moment Dishwasher Safe Apple Corer
Very good write-up. I absolutely appreciate this site.
Stick with it!
payday loan
Guide To Slot Updates: The Intermediate Guide In Slot
Updates Slot Updates
10 Wrong Answers For Common Truck Accident Attorney Questions Do You Know
The Right Ones? Truck Accidents
15 Presents For Your Slot Innovations Lover In Your Life
Slot Symbols
buy viagra online
How Window Repair Luton Was Able To Become The No.1 Trend In Social Media
Door Repair
Personal Injury Law Isn’t As Tough As You Think firm
11 Ways To Totally Defy Your Online Shopping Stores List
Acrylic Paint Brushes
This is a topic that’s near to my heart… Many thanks!
Exactly where are your contact details though?
Why Nobody Cares About Railroad Injuries Litigation Railroad Injuries Lawyers
Responsible For The Slot Trends Budget? 10 Terrible Ways To Spend Your Money Slot machine Tips
It’s The Ugly Truth About Online Shopping Sites shin guards for young players (vimeo.com)
5 Must-Know Hismphash Practices You Need To Know For 2023
Chroma 3 Case
купить диплом гознак http://www.ukr-diplom.ru .
Guide To Upvc Repairs Near Me: The Intermediate Guide On Upvc Repairs Near Me Upvc repairs near me
The 10 Most Scariest Things About Best Slot Machines Multi-Line Slots
купить аттестат за 11 класс http://ukr-diplom.ru .
Heya i’m for the first time here. I found this board and I find It truly useful & it
helped me out a lot. I hope to give something
back and aid others like you aided me.
купить диплом старого образца купить диплом старого образца .
купить диплом стоматолога http://www.ukr-diplom.ru .
What’s The Current Job Market For Which Is The
Best Online Supermarket Professionals Like? Which is the best online supermarket
10 Things You Learned From Kindergarden They’ll Help You Understand
Slot Rewards Best payouts slots