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
สำหรับคนรักฟุตบอล ที่ปรารถนาติดตามการแข่งขันฟุตบอลแบบสด ๆ ต้องเข้าไปดู doofootball (https://mecabricks.com/en/user/doofootball19).asia เว็บไซต์ที่ได้รับความนิยมสำหรับการดูบอลออนไลน์
แพลตฟอร์มนี้มอบประสบการณ์การติดตามฟุตบอลที่มีคุณภาพ พร้อมด้วยไฮไลท์ ที่อัปเดตตลอดเวลา คุณสามารถเพลิดเพลินกับการแข่งขันจากลีกยอดนิยมทั่วโลก
อย่ารอช้า คลิกไปที่ doofootball.asia วันนี้ เพื่อสัมผัสประสบการณ์การรับชมฟุตบอลที่เหนือชั้น ติดตามทุกแมตช์ที่คุณชอบ
mexican rx online: mexican pharmaceuticals online – mexico pharmacies prescription drugs
What Is Ferrari Car Key And Why Is Everyone Dissing It?
Ferrari replacement key
medication from mexico pharmacy: pharmacies in mexico that ship to usa – medication from mexico pharmacy
https://ya-drbogin-34.sgp1.digitaloceanspaces.com/the-role-of-massage-in-managing-migraines.html
The subsequent thing you want to contemplate when purchasing round for dresses is the shape of the costume.
30 Inspirational Quotes For Frost Free Fridge Freezer 50 50 fridge freezer 50 50 sale
https://ya-drbogin-22.se-sto-1.linodeobjects.com/about-us.html
Straight-leg pants create a streamlined silhouette that subtly complements the velvet burnout-print prime and jacket.
What Experts In The Field Of Nespresso Vertuo Machine Want You To Know?
nespresso coffee machines best Price
buying prescription drugs in mexico: purple pharmacy mexico price list – mexican online pharmacies prescription drugs
16 Facebook Pages That You Must Follow For Nissan Qashqai Replacement
Key-Related Businesses Nissan key reprogramming cost
mexico drug stores pharmacies: best online pharmacies in mexico – buying prescription drugs in mexico
mexican rx online: mexican drugstore online – medication from mexico pharmacy
https://ya-chinavisa-14.ewr1.vultrobjects.com/tips-and-precautions-for-using-the-taiwan-compatriot-certificate-5.html
Don’t be afraid of anything oversized—the giant bow of this gown will flatter small busts, and make waists and arms seem petite in comparison.
mexico drug stores pharmacies: mexican drugstore online – mexican online pharmacies prescription drugs
car enclosed shipping Autoshipping24.com .
10 Facts About American Fridges Freezers That Will
Instantly Put You In A Good Mood narrow american fridge freezer
If some one needs expert view on the topic of blogging and site-building then i advise him/her to go to see this blog, Keep up the nice work.
25 Surprising Facts About Manchester Windows glazing manchester
https://filedn.eu/lXvDNJGJo3S0aUrNKUTnNkb/ya-4545-1/company-registration-and-strategic-cooperation.html
The dress is figure-hugging and presents a easy silhouette.
The Top Motor Vehicle Lawsuit That Gurus Use Three Things motor vehicle accident lawsuit
purple pharmacy mexico price list: mexican drugstore online – buying from online mexican pharmacy
п»їbest mexican online pharmacies: reputable mexican pharmacies online – mexican rx online
I’ve read several excellent stuff here. Certainly price bookmarking for revisiting.
I surprise how a lot attempt you set to create one of these wonderful informative web site.
15 Top Pinterest Boards From All Time About Upvc Windows
And Doors repair upvc windows
mexican mail order pharmacies: mexico drug stores pharmacies – buying prescription drugs in mexico
https://ya-drbogin-15.fr-par-1.linodeobjects.com/the-role-of-massage-in-improving-respiratory-health.html
Another gown with ruching for you as I suppose ruched types are so flattering.
car transport across us car transport across us .
https://catering2b.netlify.app/research/catering2b-(25)
You actually want to take into consideration what’s greatest for the season, and the climate you will be sitting in.
how to transport a car cross country http://www.Car-shipping24.com .
auto transport enclosed trailer auto transport enclosed trailer .