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
Thanks for sharing your thoughts about Ionic silver.
Regards
Best casino – https://7k-kazinos-top.ru/
Thanks for every other informative site. Where else could
I get that kind of info written in such a perfect method?
I’ve a project that I’m simply now working on, and I’ve been at the glance out for such info.
промокод на подключение робокассаа промокод на подключение робокассаа .
Быстрая покупка диплома старого образца: возможные риски
Тут можно преобрести сейф оружейный купить в москве сейф для оружия купить в москве
Диплом ВУЗа купить официально с упрощенным обучением в Москве
amoxicillin 500mg price in canada: amoxil – amoxicillin online no prescription
LIVE DRAW TAIWAN
buy prednisone nz: cheap prednisone – apo prednisone
Best casino – https://psy-y.com/
Стоимость дипломов высшего и среднего образования и процесс их получения
Best casino – https://7k-cazinos-top.ru/
buy priligy: priligy – Priligy tablets
Hi, all the time i սsed too check webpage posts һere in the earlʏ hours in the break of ɗay, as і love to
gain knowledge ⲟf moгe and morе.
Feel free tο surf tο my webpage: Laskar303
Priligy tablets: dapoxetine online – Priligy tablets
robokassa промокод robokassa промокод .
can i order cipro pills
over the counter prednisone cheap: order Prednisone – prednisone 5mg capsules
darknet markets black internet dark web websites
wrapfolie helm
Nice blog here! Also your web site loads up very fast!
What host are you using? Can I get your affiliate link to your host?
I wish my web site loaded up as quickly as yours lol
I do agree with all the ideas you have introduced on your post.
They’re really convincing and will certainly work. Nonetheless, the posts are
very short for beginners. May just you please extend them a bit from next time?
Thanks for the post.
Процесс получения диплома стоматолога: реально ли это сделать быстро?
In fact when someone doesn’t understand then its
up to other people that they will help, so here it
occurs.
кухни в москве – Подбор кухонь с установкой в Москве и области.
cost generic clomid now: clomid rex pharm – order clomid without rx
Medicine information leaflet. What side effects?
what is meloxicam pills used for
All information about medication. Get now.
Hey! This is my first visit to your blog! We are a collection of
volunteers and starting a new initiative in a community in the same niche.
Your blog provided us useful information to work on. You have done
a marvellous job!
Узнай все о степени варикоцеле варикоцеле слева у мужчин
Рекомендации по безопасной покупке диплома о высшем образовании