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
לבין כל מי שעלול להפר את הפרטיות שלך. זה הוא הפתרון המושלם עבור נשים או גברים אשר יכולים ליהנות ממעט פרטיות ושקט בחיים הרועשים שלהם. הדירות פנויות עבורך בכל שעה ומוכנות עבורך, נקיות ועם כל האבזור שיהפוך את החוויה שלך לשלמה דירות הדיסקרטיות מדרום למסיבות דירה דיסקרטית בדרום תל אביב – כל תענוגות החיים במרחק נגיעה
11 “Faux Pas” Which Are Actually OK To Use With Your Mesothelioma Attorneys Asbestos settlement
https://mexicoph24.life/# purple pharmacy mexico price list
canadian pharmacy online ship to usa canadian pharmacies northwest pharmacy canada
Payday loans in Georgia
top 10 pharmacies in india http://indiaph24.store/# cheapest online pharmacy india
Online medicine home delivery
mexican online pharmacies prescription drugs mexican pharmacy mexico pharmacies prescription drugs
Everything You Need To Know About Online Shopping Sites List For
Clothes Dos And Don’ts Reflective House Number (https://vimeo.com/931757915)
Oh my goodness! Impressive article dude! Thank you so much,
However I am experiencing problems with your RSS. I don’t
understand why I am unable to join it. Is there anyone else getting the same RSS issues?
Anyone who knows the answer will you kindly respond?
Thanx!!
reputable indian pharmacies Cheapest online pharmacy indian pharmacy paypal
india online pharmacy indian pharmacy world pharmacy india
How To Outsmart Your Boss In Pvc Window Repairs repairer
https://mexicoph24.life/# medication from mexico pharmacy
mexican drugstore online: mexico pharmacy – mexico drug stores pharmacies
9 . What Your Parents Teach You About Nespresso Pixie Machine Nespresso Pixie Machine
20 Best Tweets Of All Time About Programing Keys Transponder key Programmer
How Do You Know If You’re Ready For Car Locksmith Emergency Car Locksmith
http://mexicoph24.life/# mexico pharmacy
10 Inspirational Graphics About Automotive
Locksmith Near Me auto Keys locksmith
Online medicine home delivery https://indiaph24.store/# indian pharmacy paypal
india pharmacy
Don’t Believe These “Trends” Concerning Marc Jacobs Camera Bag Marc Jacobs Tote Bag Tk Maxx
Why Semi Truck Lawyer Will Be Your Next Big Obsession Semi Truck accident
10 Quick Tips About Replacement Bmw Key replacement key for Bmw
http://mexicoph24.life/# mexican pharmaceuticals online
The 10 Most Terrifying Things About Treadmills For Sale UK treadmills for sale uk
It’s awesome to visit this website and reading the views of all friends regarding this paragraph, while I
am also keen of getting know-how.
What Will Wall Mounted Electric Fires Be Like In 100 Years?
Wall Hung Electric Fire
best online foreign pharmacy
Could Espresso Machine Be The Key To Dealing With 2023?
Espresso Coffee Machines uk
cheapest on line valtrex without a prescription