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
medication from mexico pharmacy: buying prescription drugs in mexico online – mexican rx online
Why We Are In Love With Vintage Leather Couch (And You Should, Too!) Vintage Leather Sofa
https://bogin14.research.au-syd1.upcloudobjects.com/research/bogin14-(159).html
Weddings may be very totally different right now, depending on the place you live.
buying prescription drugs in mexico: mexican rx online – mexico drug stores pharmacies
mexican drugstore online: medicine in mexico pharmacies – mexican online pharmacies prescription drugs
What’s The Job Market For Double Glazed Window Repairs Professionals?
Double Glazed Window Repairs
medicine in mexico pharmacies: mexican rx online – medication from mexico pharmacy
mexican online pharmacies prescription drugs: reputable mexican pharmacies online – mexican border pharmacies shipping to usa
Find Out What Boat Accident Lawsuit Tricks The Celebs Are Using boat Accident lawyers (http://www.youwantech.com)
shipping my car cross country shipping my car cross country .
mexican border pharmacies shipping to usa: mexican pharmaceuticals online – pharmacies in mexico that ship to usa
shipping a car cross country shipping a car cross country .
cross country car transport cross country car transport .
shipping a vehicle cross country shipping a vehicle cross country .
5 Must-Know 5kw Multi Fuel Stoves Defra Approved
Practices For 2023 349338.xyz (https://www.349338.xyz/qd2yz37-j66-9c6L7-5ymzxm-x3D5-3332/)
vehicle shipping between states Car-shipping24.com .
medication from mexico pharmacy: mexico pharmacies prescription drugs – purple pharmacy mexico price list
mexican rx online: mexico drug stores pharmacies – mexico drug stores pharmacies
Everything You Need To Know About Electric Travel
Wheelchair chairs electric
buying prescription drugs in mexico online: mexico pharmacies prescription drugs – mexico drug stores pharmacies
What Window Repairs London Experts Want You To Know upvc window repairs london
mexican rx online: mexico drug stores pharmacies – mexican mail order pharmacies
10 Quick Tips For Cost Of Replacement Car Key replacement car key costs
10 Tell-Tale Signs You Must See To Get A New Electric Treadmill Space saving
electric treadmill (enfogentraining.com)
Привет!
Мы изготавливаем дипломы психологов, юристов, экономистов и других профессий по выгодным ценам.
formfinance.ru/byistroe-oformlenie-diplomov-nadezhno-i-legalno
Hi there! This is kind of off topic but I need
some advice from an established blog. Is it tough to
set up your own blog? I’m not very techincal but I can figure things out pretty quick.
I’m thinking about setting up my own but I’m not sure where to begin. Do you have any ideas or suggestions?
Thank you
buying prescription drugs in mexico: reputable mexican pharmacies online – medication from mexico pharmacy
shipping vehicle cross country shipping vehicle cross country .
medication from mexico pharmacy: reputable mexican pharmacies online – mexican rx online
https://ya-chinavisa-13.sos-ch-dk-2.exo.io/the-difference-between-taiwan-compatriot-permit-and-travel-permit.html
Don’t be involved with having everything match completely.