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
Полезная информация как официально купить диплом о высшем образовании
Обработка квартир после пожара https://spec-uborka-posle-pozhara.ru/
10 Things You Learned In Kindergarden To Help You Get
Started With All Terrain Prams pushchair for rough terrain
supplements for ed: cheap prescription drugs – ed drug prices
cure for ed low cost pharmacy ed pumps
Think You’re Ready To Start Do You Need A Driving License For A Scooter?
Take This Quiz testynaprawojazdy eu (Delores)
с Днем рождения директору
10 Wrong Answers To Common Asbestos Attorney Questions: Do You Know The
Right Answers? Mesothelioma Lawyer
new ed treatments http://indianpharm24.pro/# top 10 pharmacies in india
mexican mail order pharmacies mexico pharmacy cheap reputable mexican pharmacies online
10 Reasons That People Are Hateful To Leather Chesterfield Leather Chesterfield material chesterfield sofa (Cerys)
discount prescription drugs http://mexicanpharm24.cheap/# mexican border pharmacies shipping to usa
Why Truck Accident Lawyers Is So Helpful During COVID-19 truck accident attorneys
купить аттестат за 8 классов prema-diploms.ru .
диплом о высшем образовании с занесением в реестр стоимость prema-diploms.ru .
Lavazza Coffee Beans 1kg Techniques To Simplify Your Daily Lifethe One Lavazza Coffee Beans 1kg Trick
That Everyone Should Know coffee Beans 1kg
Need Inspiration? Check Out Programming A Car Key Mobile Car key programming near me
Надежный поставщик недорогой строительной арматуры. Качественная арматурная сталь а500с по выгодной цене с доставкой куплю арматуру
https://mexicanpharm24.cheap/# mexican drugstore online
non prescription ed pills
indian pharmacy paypal Indian pharmacy online Online medicine home delivery
25 Shocking Facts About Best Cot Newborn baby cots online
The 3 Most Significant Disasters In Car Keys Programming History Key Reprogramming
https://pharm24.pro/# best erection pills
generic ed pills
herbal remedies for ed: cheap pharmacy online – prescription drugs online without
male enhancement: cheap pharmacy – buy ed pills
10 Things Everybody Hates About Wall Mounted Electric Fires log effect wall mounted electric
fires – Ingrid,
Hi, I read your blog like every week. Your writing style is witty,
keep up the good work!
http://indianpharm24.pro/# indian pharmacy online
medications for
pet antibiotics without vet prescription https://indianpharm24.pro/# best online pharmacy india
Как официально купить аттестат 11 класса с упрощенным обучением в Москве