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
canadian pharmacy ed medications: CanadianMdPharm – canadian drugs
купить программу 1с бухгалтерия купить программу 1с бухгалтерия .
canadian pharmacy: Canadian Md Pharm – canadian pharmacy 24h com
1с предприятие купить программу https://svstrazh.forum24.ru/?1-3-0-00000323-000-0-0-1737110513/ .
купить программу 1с бухгалтерия купить программу 1с бухгалтерия .
Mexican Easy Pharm: Mexican Easy Pharm – Mexican Easy Pharm
выведение из запоя воронеж стационар выведение из запоя воронеж стационар .
https://writeablog.net/i676tpbaz7
Medicines information for patients. Cautions.
metoprolol succ coupon
Everything trends of medication. Get information here.
canadian pharmacy ltd https://mexicaneasypharm.shop/# reputable mexican pharmacies online
top 10 pharmacies in india
Indian Cert Pharm: Indian pharmacy international shipping – Best Indian pharmacy
настроить 1с бухгалтерия цена настроить 1с бухгалтерия цена .
It’s great that you are getting ideas from this post
as well as from our discussion made here.
Thanks for the valuable article. More at online pokies bonuses NZ
купить программу 1с бухгалтерия купить программу 1с бухгалтерия .
программа 1с купить http://svstrazh.forum24.ru/?1-3-0-00000323-000-0-0-1737110513/ .
Online pharmacy: Best online Indian pharmacy – Indian Cert Pharm
canadian valley pharmacy: 77 canadian pharmacy – 77 canadian pharmacy
1с бухгалтерия купить 1с бухгалтерия купить .
Indian Cert Pharm: Indian Cert Pharm – IndianCertPharm
настройка 1с бухгалтерии цена настройка 1с бухгалтерии цена .
adderall canadian pharmacy https://canadianmdpharm.online/# canadapharmacyonline com
top online pharmacy india
Best Indian pharmacy: Indian Cert Pharm – Online pharmacy
reddit canadian pharmacy: online canadian drugstore – canadian pharmacy store
программа 1с купить с установкой программа 1с купить с установкой .
I got this web page from my friend who told me on the
topic of this web site and at the moment this time I
am browsing this site and reading very informative
articles at this place.
buy drugs from canada: Canadian Md Pharm – best canadian online pharmacy reviews
I found this very interesting. Check out رنگ ضد حریق for more
Can I simply say what a relief to uncover somebody who actually
understands what they’re talking about on the web.
You certainly know how to bring a problem to light and make it important.
More people really need to look at this and understand this side
of the story. It’s surprising you are not more popular given that you most
certainly have the gift.
https://mexicaneasypharm.shop/# Mexican Easy Pharm
canadian pharmacy