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
Всем привет! Может кто знает, где найти полезные блоги о недвижимости? Пока нашел https://titovloft.ru
mexico drug stores pharmacies
https://cmqpharma.com/# п»їbest mexican online pharmacies
mexican rx online
Serxio Ramos Garsiya https://serxio-ramos.com ispaniyalik futbolchi, himoyachi. Ispaniya terma jamoasining sobiq futbolchisi. 16 mavsum davomida u “Real Madrid”da markaziy himoyachi sifatida o’ynadi.
buying prescription drugs in mexico
http://cmqpharma.com/# mexican drugstore online
medicine in mexico pharmacies
Upvc Windows Near Me Tools To Streamline Your Everyday Lifethe Only Upvc Windows Near Me Trick That Should Be Used By Everyone Learn Upvc windows near me
п»їbest mexican online pharmacies
http://cmqpharma.com/# pharmacies in mexico that ship to usa
mexican drugstore online
where buy cheap avodart without a prescription
when to take prednisone together
mexican mail order pharmacies
https://cmqpharma.online/# п»їbest mexican online pharmacies
pharmacies in mexico that ship to usa
п»їbest mexican online pharmacies
https://cmqpharma.online/# reputable mexican pharmacies online
purple pharmacy mexico price list
How To Explain Malpractice Claim To A 5-Year-Old malpractice attorneys
It’s a shame you don’t have a donate button! I’d certainly donate to this fantastic blog!
I suppose for now i’ll settle for book-marking and adding
your RSS feed to my Google account. I look forward to brand new updates and
will share this website with my Facebook group. Talk
soon!
can i get cheap levaquin tablets
You’ll Never Guess This Window Replacement Near Me’s
Secrets Window Replacement Near Me
mexican mail order pharmacies
https://cmqpharma.online/# mexico drug stores pharmacies
mexican online pharmacies prescription drugs
Get the latest https://mesut-ozil-uz.com Mesut Ozil news, stats, photos and more.
Companionship for Mental Health Challenges: jydollIndividuals facing mental health challenges, such as depression or severe social withdrawal
Latest news about Pele https://mesut-ozil-uz.com, statistics, photos and much more. Get the latest news and information about football legend Pele.
Tubba https://tubba.ru предлагает бесплатный просмотр фильмов и сериалов без регистрации и смс.
The Reason Why You’re Not Succeeding At Malpractice Attorneys malpractice law firm
Магазин металлов изо разноцветных равно чёрных металлов “STORM”
https://stormbrand.ru/
mexican online pharmacies prescription drugs
http://cmqpharma.com/# best online pharmacies in mexico
mexican mail order pharmacies
Explore the extraordinary journey of Kylian Mbappe https://mbappe-real-madrid.com, from his humble beginnings to global stardom.
how can i get cheap tegretol for sale
Магазин заготовок с цветных равно чёрных металлов “STORM”
https://stormbrand.ru/
mexican mail order pharmacies
http://cmqpharma.com/# mexico pharmacies prescription drugs
pharmacies in mexico that ship to usa
как справиться с https://bereg.uzhgorod.ua/analna-probka чувством
стеснения? что выгоднее – купить
впервые?
how to buy cheap macrobid without rx
This is the perfect web site for anybody who really wants to
find out about this topic. You know so much its almost tough to argue with you (not that I actually will need to…HaHa).
You definitely put a brand new spin on a subject that’s been discussed for years.
Excellent stuff, just wonderful!
The Powerball jackpot was eluded after once more Monday, but
there was a single lucky winner in Southern California
who has a winning ticket worth $1 million.
Here is my website: Gladis