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
Very rapidly this web site will be famous amid all blogging visitors, due to it’s good articles or reviews
It’s very simple to find out any topic on web as
compared to books, as I found this post
at this website.
It’s great that you are getting ideas from this piece of writing as well as from our argument made
here.
Геймеры могут играть с любых портативных устройств прямо в браузере.
Пошаговая инструкция по официальной покупке диплома о высшем образовании
Small Gas Patio Heater Techniques To Simplify Your Daily Lifethe One Small Gas Patio Heater Technique Every Person Needs To Know small
gas patio heater – Larae,
So You’ve Bought Private ADHD Assessment London … Now What?
private adhd assessment reading (privateadhdassessment68022.yomoblog.com)
You Are Responsible For An Key Fob Repair Budget? 12 Top Notch Ways To Spend Your Money Repair Car
Key Fob (http://Autoban.Lv)
возврат займа нужен ли кассовый чек
herbal remedies for ed http://pharm24.pro/# ed in men
Also visit my web blog; Explore Daycares Locations
(http://anticonvulsants.com/)
Excellent way of telling, and good post to obtain data regarding my presentation subject matter, which i
am going to present in institution of higher education.
ed dysfunction http://pharm24.pro/# erectile dysfunction drug
Thanks for the good writeup. It in fact was a leisure account
it. Glance complicated to more brought agreeable from you!
However, how can we keep up a correspondence?
Are You Getting Tired Of Replacement Glass? 10 Inspirational Sources That
Will Bring Back Your Love glass window replacement cost
Приобретение диплома ПТУ с сокращенной программой обучения в Москве
What’s The Current Job Market For L Shaped Sofa Couch Professionals?
L Shaped Sofa couch
Полезные советы по покупке диплома о высшем образовании без риска
16 Facebook Pages You Must Follow For Mystery Box-Related Businesses best mystery box online shop
Why New Audi Key Should Be Your Next Big Obsession New Audi Keys
Five Killer Quora Answers On Sofas 2 Seater Fabric sofas 2 seater Fabric
9 Signs That You’re The Sprt Toto 4d Expert 첫가입 꽁머니, Rodney,
Why viewers still use to read news papers when in this technological globe all is accessible on web?
https://dtsg.drawsgp.info/
купить виртуальные номера
top online pharmacy india Best Indian pharmacy top 10 online pharmacy in india
Официальная покупка школьного аттестата с упрощенным обучением в Москве
Не прошедшие отбор сайты вносятся в Единый реестр запрещенной информации.
indianpharmacy com: Best online Indian pharmacy – world pharmacy india
Покупка диплома о среднем полном образовании: как избежать мошенничества?
Leather Couches For Sale Tools To Streamline Your Daily Lifethe One
Leather Couches For Sale Trick That Should Be Used By Everyone Be Able To leather Couches for sale