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
The 9 Things Your Parents Taught You About Veterans Disability Lawyer Veterans Disability Lawyer
14 Businesses Doing An Amazing Job At Adhd Assessment Adults adhd assessment for adults what to expect
The Top Reasons Why People Succeed In The ADHD Diagnostic Industry adhd private diagnosis cost (https://freemaple.today/bbs/board.php?Bo_table=free&wr_id=204418)
Cash-Back Cc Rewards – Now Back With A Vengeance 버팀목 대출
5 Killer Quora Answers On Double Glazed Near Me Double Glazed Near Me
Nicely put, Regards.
Upvc Windows Near Me Tools To Make Your Everyday Lifethe Only Upvc Windows Near
Me Trick That Should Be Used By Everyone Learn upvc windows near me [http://bitetheass.com/]
Why You Should Focus On The Improvement Of
Demo Slot Gatot Kaca demo slot gatotkaca pragmatic
How Glazing Repair Became The Hottest Trend In 2023 https://www.jerealas.top
Keep on working, great job!
The Three Greatest Moments In Door Repair Near Me History window replacement near me (Viola)
What’s The Current Job Market For Window Doctor Near Me Professionals Like?
window doctor near Me
The 3 Most Significant Disasters In Personal Injury Attorney History Personal Injury Attorneys
See What Veterans Disability Lawyer Tricks The Celebs Are Utilizing lawyer
A Positive Rant Concerning Motorcycle Accident Lawsuit
motorcycle Accident lawyer
Drug information. Long-Term Effects.
where to get cheap erythromycin for sale
Some trends of meds. Read information here.
7 Tips About Zeus Slot Pragmatic That Nobody Can Tell You Demo Slot Cq9 Zeus
5 Killer Quora Answers To Veterans Disability Attorneys Veterans Disability Attorney
How To Tell If You’re At The Right Level To Go
After Zeus Slot Machines trik Slot zeus Pragmatic
Thanks a lot! Plenty of facts.
The 10 Most Scariest Things About Main Sweet Bonanza Demo Main Sweet Bonanza Demo (Active.Popsugar.Com)
What’s The Job Market For Double Glazed Window Repairs Professionals?
double glazed window Repairs
A List Of Common Errors That People Make Using Nissan Key Fob
Replacement Nissan Pulsar Key Replacement
9 . What Your Parents Teach You About Double Glazed
Window Suppliers Near Me double glazed window suppliers near me
Just wish to say your article is as astonishing.
The clarity in your publish is just nice and i could think you are
an expert in this subject. Well along with your permission allow me to take hold of
your feed to stay up to date with impending post.
Thank you 1,000,000 and please keep up the rewarding work.
20 Fun Informational Facts About Malpractice Compensation Rock Falls Malpractice Attorney
The 9 Things Your Parents Taught You About Bentley Key
Fob Programming Bentley Key fob
Please let me know if you’re looking for a
article writer for your weblog. You have some really good articles and I feel I would
be a good asset. If you ever want to take some of the load off, I’d
absolutely love to write some content for your blog in exchange for a link back to mine.
Please send me an e-mail if interested. Kudos!
Pills information. Brand names.
where can i get generic minocycline without rx
Everything information about medicament. Get now.
10 Things We All Hate About Private ADHD
Assessment Online Private Adhd Assessment Exeter – https://Speedgh.Com/Index.Php?Page=User&Action=Pub_Profile&Id=1149006,