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
Watch This: How Medical Malpractice Attorneys Is Taking Over The World And What You Can Do About It Medical malpractice Law Firms
10 Unexpected Malpractice Claim Tips mcfarland malpractice attorney
We are a group of volunteers and opening a new scheme in our community.
Your website provided us with valuable information to
work on. You have done a formidable job and our entire community
will be grateful to you.
20 Irrefutable Myths About Attorney: Busted malpractice lawsuits
Think You’re The Perfect Candidate For Window Glass Replacement?
Try This Quiz window glass replacements (http://www.sitiosecuador.com)
15 Best Twitter Accounts To Discover More About Pvc Window Repairs http://www.257634.xyz
The 10 Most Terrifying Things About Medical Malpractice Law Medical Malpractice
Medicines prescribing information. Long-Term Effects.
how to get generic glucophage without prescription
All about medicine. Read here.
The 9 Things Your Parents Teach You About Double.Glazing Near Me near
10 Mistaken Answers To Common Double Glazing Supplies Near Me Questions: Do
You Know The Right Ones? double glazing window Handles
15 Up-And-Coming Door Repair Near Me Bloggers You Need To Follow
window replacement Near Me (http://www.Stes.tyc.edu.tw)
What Freud Can Teach Us About Mobility Scooters Folding folding
mobility scooter for heavy person uk; https://ife-ifem-hrc2024.Coconnex.Com/node/1063610,
See What Double Glazing Units Near Me Tricks The Celebs Are Making Use
Of double glazing Units near me
You’ll Be Unable To Guess Adult Adhd Assessment Uk’s Tricks adult adhd
assessment uk (Jerrold)
5 Killer Quora Answers On Double Glazed Near Me double glazed near me
3 Reasons Commonly Cited For Why Your Treatment Of Adult
ADHD Isn’t Working (And How To Fix It) symptoms of untreated adhd – http://jejucordelia.com/eng/bbs/Board.php?bo_table=review_e&wr_id=464549 –
You Will Meet You The Steve Jobs Of The Medical Malpractice Attorney Industry Medical malpractice lawyer
The Top Kia Replacement Key Gurus Are Doing 3 Things 2010 kia soul key Fob replacement
Accident Legal Explained In Fewer Than 140 Characters Accident Law Firm
Why Car Boot Mobility Scooters Might Be Your Next Big Obsession arlennizo.top
In this chart, the dealer stands on Soft 17, andd doubling down is allowed.
My webpage; 비트코인 지갑 복구
Backlink Relevance – This Matter Where Your Backlink
Is Printed? 백링크 작업
https://boostlineketo.com is a popular dietary supplement that is designed to help individuals achieve their weight loss goals through the process of ketosis.
See What Liability Act Fela Tricks The Celebs Are Making Use Of Act Fela
20 Trailblazers Leading The Way In Childrens Bunk Bed Junko
Hello my family member! I want to say that this post is amazing, great written and include almost all vital infos.
I’d like to look extra posts like this .
This Is The Ultimate Cheat Sheet For Boat Accident Attorney Boat accident law firm
You’ll Be Unable To Guess Window Doctor Near Me’s Tricks Window doctor near Me
I enjoy what you guys are usually up too. Such clever work and reporting!
Keep up the good works guys I’ve incorporated you guys
to my blogroll.
7 Simple Tricks To Totally Enjoying Your Window Glass Replacement replacement
window pane (Geoffrey)