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
mexican mail order pharmacies: mexican pharmacy – pharmacies in mexico that ship to usa
выездной ремонт стиральных машин http://centr-remonta-stiralnyh-mashin.ru/ .
mexican mail order pharmacies: mexican pharmacy online – medication from mexico pharmacy
Good day very cool site!! Guy .. Excellent .. Superb .. I’ll bookmark your web site and take the feeds additionally?
I am glad to seek out so many helpful info here within the publish, we want work out more techniques on this regard,
thanks for sharing. . . . . .
my blog fitspresso customer reviews
This article will assist the internet people for setting up new web site or
even a blog from start to end.
mexican border pharmacies shipping to usa: mexico drug stores pharmacies – mexican rx online
10 Bunk Bed Online Store Projects Related To Bunk Bed Online Store
To Extend Your Creativity https://www.eddafay.top/
срочно отремонтировать стиральную машину https://centr-remonta-stiralnyh-mashin.ru .
Experience the thrill of betting on cricket matches in the heart of the United Arab Emirates. Get ready to win big and cheer for your favorite teams https://1xbett1xbetc1xbetir4.ru/!
Всем привет! Может кто знает, где найти разные статьи о недвижимости? Пока нашел https://teplohod-denisdavidov.ru
buying prescription drugs in mexico
https://cmqpharma.com/# mexican drugstore online
pharmacies in mexico that ship to usa
отремонтировать стиральную машину в москве отремонтировать стиральную машину в москве .
xxx
buying prescription drugs in mexico online
https://cmqpharma.online/# pharmacies in mexico that ship to usa
buying prescription drugs in mexico online
not safe
mexican mail order pharmacies
http://cmqpharma.com/# buying from online mexican pharmacy
buying prescription drugs in mexico online
reputable mexican pharmacies online: cmq mexican pharmacy online – buying prescription drugs in mexico online
I’m not that much of a online reader to be honest but your sites really nice, keep it up!
I’ll go ahead and bookmark your site to come back later on. Many thanks
buying prescription drugs in mexico online
https://cmqpharma.com/# medicine in mexico pharmacies
mexico drug stores pharmacies
ремонт стиральных машин в москве рядом http://centr-remonta-stiralnyh-mashin.ru/ .
buying prescription drugs in mexico online
http://cmqpharma.com/# mexico drug stores pharmacies
mexican mail order pharmacies
buying from online mexican pharmacy: online mexican pharmacy – п»їbest mexican online pharmacies
What’s up everybody, here every person is sharing these know-how, so it’s good to read this
blog, and I used to pay a quick visit this
webpage every day.
reputable mexican pharmacies online
https://cmqpharma.online/# mexican pharmaceuticals online
mexican mail order pharmacies
buying prescription drugs in mexico
http://cmqpharma.com/# best online pharmacies in mexico
п»їbest mexican online pharmacies
Incredible points. Great arguments. Keep up the good work.
9 Methods To Generate Infinite Traffic 검색엔진최적화
배우기, https://telegra.ph/Top-7-Mistakes-You-Simply-Should-Avoid-In-Seo-Link-Building-06-19,
Guide To Upvc Windows Near Me: The Intermediate Guide To Upvc Windows Near Me upvc windows
near me (tourdeskhawaii.com)
where can i buy tetracycline ointment
mexican rx online
https://cmqpharma.online/# purple pharmacy mexico price list
best online pharmacies in mexico