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://canadianmdpharm.shop/# canadian pharmacy oxycodone
maple leaf pharmacy in canada
Mexican Easy Pharm: reputable mexican pharmacies online – Mexican Easy Pharm
IndianCertPharm: Indian pharmacy that ships to usa – Indian pharmacy international shipping
программа 1с купить программа 1с купить .
canadian pharmacy online reviews: CanadianMdPharm – canadian pharmacy prices
medication from mexico pharmacy: buying from online mexican pharmacy – mexico drug stores pharmacies
After going over a handful of the articles on your site, I really
appreciate your technique of writing a blog. I bookmarked it
to my bookmark website list and will be checking back in the
near future. Please visit my website too and let me know
what you think.
canada pharmacy online: CanadianMdPharm – onlinecanadianpharmacy 24
купить программу 1с бухгалтерия купить программу 1с бухгалтерия .
купить 1с бухгалтерия купить купить 1с бухгалтерия купить .
купить 1с бухгалтерия 8.3 проф версия цена купить 1с бухгалтерия 8.3 проф версия цена .
Your method of explaining all in this post is truly pleasant,
all be capable of without difficulty know it, Thanks a lot.
1с предприятие купить программу http://www.svarog.forum24.ru/?1-0-0-00000330-000-0-0 .
The Ultimate Guide To Software For SEO Ranking Seo Tool
legitimate canadian online pharmacies https://canadianmdpharm.com/# canadian pharmacy meds review
Online medicine home delivery
Welcome bonuses at Bitcoin casinos can come in many forms,
including free spins, deposit bonuses, or a combination of both.
Best Indian pharmacy: Indian Cert Pharm – Indian Cert Pharm
Viagra * Cialis * Levitra
All the products you are looking an eye to are currently close by for the duration of 1+1.
4 more tablets of one of the following services: Viagra * Cialis * Levitra
https://xn--2i0bm4p0sf2wh7vdmsy.net
http://www.freshlyme.ru — Всё для вашего идеального кухонного пространства.
купить программу 1с бухгалтерия купить программу 1с бухгалтерия .
1с предприятие купить программу 1с предприятие купить программу .
Excellent website. Plenty of useful info here.
I’m sending it to some pals ans additionally sharing in delicious.
And certainly, thanks on your effort!
http://www.freshlyme.ru — Доступный сервис для проектирования кухни.
canadian pharmacy 24h com https://mexicaneasypharm.com/# Mexican Easy Pharm
Online medicine order
программа 1с купить с установкой программа 1с купить с установкой .
canadian pharmacy india https://canadianmdpharm.online/# canadian drugs pharmacy
buy prescription drugs from india
вывод из запоя воронеж вывод из запоя воронеж .
Indian pharmacy international shipping: india pharmacy – Indian Cert Pharm
safe online pharmacies in canada https://canadianmdpharm.com/# canada discount pharmacy
reputable indian online pharmacy
вывод из запоя стационар вывод из запоя стационар .