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
Good day! This is my first visit to your blog! We are a team
of volunteers and starting a new project in a community in the same niche.
Your blog provided us valuable information to work on. You have done a wonderful job!
оборудование конференц залов оборудование конференц залов .
конференц-залы под ключ конференц-залы под ключ .
10 Facts About Slot Updates That Will Instantly Get You Into A Great Mood entertaining slots
Online Shopping Websites Clothes Tools To Ease Your Daily Life Online
Shopping Websites Clothes Trick That Every Person Should Learn online shopping websites clothes
The Myths And Facts Behind Loft Beds With Desk Low loft bed frame
Window Repair Near Me Techniques To Simplify Your Daily Lifethe One Window Repair Near Me Trick That Every Person Should Be Able To Window Repair Near Me
can you buy generic tadacip for sale
See What Adhd Assessment Tricks The Celebs Are Utilizing adhd Assessment women
20 Irrefutable Myths About Mesothelioma Litigation: Busted Mesothelioma lawyer
Who Is Responsible For An Cerebral Palsy Litigation Budget?
12 Best Ways To Spend Your Money Cerebral Palsy Lawsuits, Losguerrerosdeoracion.Com,
nettilinkki
So , You’ve Purchased Slot Sound Effects … Now What?
Slot wins
Lorem Ipsum has been the industry’s standard dummy text https://propcraft.pl/gaavnjn of type and scrambled it to make a type specimen book.
can i buy cheap cleocin
The Comprehensive Guide To Machu Picchu Coffee Beans 1kg 1kg arabica coffee beans (r.asajikan.jp)
ค้นหา โลกแห่งการ ‘SlotXO’
‘SlotXO’ เป็นการทดลอง โลกออนไลน์ที่เปิดกว้างให้กับผู้เล่นจำนวนมาก ด้วยความสะดวกและความปลอดภัยที่เหนือระดับ ผู้เล่นสามารถดำเนินการ เกมสล็อตออนไลน์ได้ทุกที่ทุกเวลา โดยเพียงแค่มีอุปกรณ์ที่เชื่อมต่ออินเทอร์เน็ต
ไม่ว่าจะเป็นสมาร์ทโฟน แท็บเล็ต หรือคอมพิวเตอร์ก็ตาม
‘SlotXO’ นำเสนอ เกมสล็อตที่หลากหลายและน่าตื่นเต้น ด้วยกราฟิกและเอฟเฟกต์เสียงที่สมจริง ผู้เล่นจะได้เพลิดเพลิน กับประสบการณ์การเล่นเกมที่เหมือนจริงเสมือนอยู่ในบ่อนคาสิโนจริง ๆ นอกจากนี้ยังมีโบนัสและรางวัลมากมายให้ผู้เล่นได้ใช้ประโยชน์ ซึ่งจะทำให้การเล่นเกิดความตื่นเต้นและสนุกสนานยิ่งขึ้น
ด้วยการคุ้มครอง ความปลอดภัยที่เข้มงวด ‘SlotXO’ ยังมีระบบการทำการตรวจสอบ ที่ปลอดภัยและเชื่อถือได้ ผู้เล่นสามารถเบิก เงินได้อย่างสะดวก รวดเร็ว และปลอดภัย นับเป็นการเปิดโอกาส ให้ผู้เล่นได้เพลินใจ กับประสบการณ์การเล่นเกมสล็อตออนไลน์ที่ดีที่สุด
ในภาพรวม ‘SlotXO’ เป็นหน้าต่าง
ที่น่าสนใจสำหรับผู้ที่หลงใหล การเล่นเกมสล็อตออนไลน์ ด้วยความครบครัน
ของเกม ความปลอดภัยสูง และชิง
ในการรับรางวัล ซึ่งจะทำให้ผู้เล่นได้รับความตื่นเต้น ในการเล่นเกมอย่างแน่นอน
my blog post ข้อกำหนดในการเดิมพัน – hikvisiondb.webcam,
Why people stiⅼl սse to reɑd news paperrs ԝhen inn this technplogical ԝorld the wһole tһing іs available оn web?
Feel frewe tⲟ surf to my webpage … slot gacor
8 Tips To Up Your Semi Truck Lawyer Game semi Truck accident Attorneys
20 Things You Need To Be Educated About Upvc Windows Near
Me Upvc Window Repair
Guide To Online Shopping Uk Cheap: The Intermediate Guide
Towards Online Shopping Uk Cheap Online Shopping Uk Cheap (http://Www.Russiacompany.Ru)
This website was… how do I say it? Relevant!! Finally I have found something that helped me.
Thank you!
where buy tadacip without rx
5 Killer Quora Answers To Sofas Sale sofas sale
5 Killer Quora Answers On Best 18 Wheeler Accident Attorneys 18 Wheeler Accident Attorney (http://Www.Google.Com)
What’s The Reason? Erb’s Palsy Case Is Everywhere
This Year erb’s Palsy lawyers
Hi! where can i buy cheap finasteride tablets excellent website.
how can i get cheap lexapro pills
Auto Accident Lawyers Tools To Ease Your Everyday Lifethe
Only Auto Accident Lawyers Technique Every Person Needs To Know auto accident lawyers
You’ll Never Be Able To Figure Out This Shopping Online Uk To Ireland’s Secrets shopping
online uk to ireland (Arielle)