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
вывод из запоя в люберцые вывод из запоя в люберцые .
india pharmacy mail order: Indian pharmacy worldwide delivery – world pharmacy india
For latest information you have to go to see web and on web I found this site as a finest
web page for newest updates.
Take a look at my blog post: Mpo Slot
https://pharm24.pro/# buy erection pills
online drug store
Сервисный центр предлагает ремонт ginzzu s4010 ремонт ginzzu s4010 цены
solutions for ed http://mexicanpharm24.cheap/# reputable mexican pharmacies online
prescription without a doctor’s prescription http://pharm24.pro/# google viagra dosage recommendations
Demisting Double Glazing Tools To Improve Your Daily Lifethe One Demisting Double Glazing
Trick Every Person Should Know Demisting Double Glazing
10 Locations Where You Can Find Locksmith Automotive
Near Me automotive locksmith near me prices (Catharine)
Way cool! Some extremely valid points! I appreciate you writing this article plus the rest of the
site is extremely good.
top online pharmacy india Indian pharmacy online reputable indian pharmacies
Независимо от того, являетесь ли вы опытным пользователем или только начинаете познавать блокчейн, MetaMask помогает вам подключиться к децентрализованной сети: новому интернету metamask download android
male enhancement: cheap drugs – natural ed
canadian drug cheap pharmacy online prescription for ed meds
I do trust all of the ideas you have presented for your
post. They are really convincing and can definitely work.
Nonetheless, the posts are too quick for beginners.
Could you please prolong them a bit from next time?
Thanks for the post.
mexican rx online: mexico pharmacy – п»їbest mexican online pharmacies
10 Things You Learned In Kindergarden That’ll Help You With Mesothelioma Lawyers
Mesothelioma support lawyer
Как безопасно купить диплом колледжа или ПТУ в России, что важно знать
What phrase… super, excellent idea
[url=https://tcicgcluj.ro]https://tcicgcluj.ro[/url]
Легальные способы покупки диплома о среднем полном образовании
india pharmacy mail order Indian pharmacy international shipping mail order pharmacy india
Сервисный центр предлагает замена заднего стекла asus transformer pad tf300tg замена переднего стекла asus transformer pad tf300tg
http://pharm24.pro/# pet meds without vet prescription
ed natural treatment
What’s The Current Job Market For Best Coffee Machine Uk Professionals
Like? best coffee machine uk (Carroll)
buying from online mexican pharmacy: mexico pharmacy cheap – buying prescription drugs in mexico online
Five Essential Qualities Customers Are Searching For In Every Train Injury Claim Semi Truck Attorney
вывод из запоя на дому в люберцах вывод из запоя на дому в люберцах .
нарколог в люберцах kapelnica-ot-zapoya-lyubercy11.ru .
what causes ed http://pharm24.pro/# how to help ed
Сервисный центр предлагает ремонт toshiba satellite c650d ремонт toshiba satellite c650d цены