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
Have a look at my website local hookups near me
Магазин спортивной фармакологии – OxyShop. Это всемирно известный надёжный поставщик качественных препаратов для спортсменов спортивная фарма форум
У вас есть возможность обратиться к администрации через электронную почту.
https://canadaph24.pro/# canada drugs online reviews
canadian pharmacy online Certified Canadian Pharmacies canadian discount pharmacy
Excellent article. I certainly appreciate this website. Continue the
good work!
best india pharmacy indian pharmacy indian pharmacies safe
medication from mexico pharmacy mexican pharmacy mexican rx online
online shopping pharmacy india https://indiaph24.store/# buy prescription drugs from india
online pharmacy india
The Reasons You Shouldn’t Think About Improving Your CSGO New Case
Revolution case
https://canadaph24.pro/# canadian drug stores
Hey, I think your website might be having browser compatibility
issues. When I look at your blog in Firefox, it looks fine but when opening in Internet Explorer, it has some overlapping.
I just wanted to give you a quick heads up! Other then that, very
good blog!
Online medicine order Cheapest online pharmacy reputable indian online pharmacy
mexican drugstore online: mexico pharmacy – buying from online mexican pharmacy
Yes! Finaⅼly sometһing abօut google trending news.
mу site: какво се случва с интернет днес
See What Vauxhall Key Fobs Tricks The Celebs Are Using Vauxhall key fob
medication from mexico pharmacy buying prescription drugs in mexico medication from mexico pharmacy
10 Things You Learned From Kindergarden That Will Help You Get Uk Online Phone Shopping Sites High-Quality Painting Brush
This Is The Ugly Real Truth Of Online Shopping
nds 117bc icv
https://canadaph24.pro/# maple leaf pharmacy in canada
9 . What Your Parents Taught You About Auto Accident Lawsuit
Auto Accident Lawsuit
The Most Successful Places To Buy Electric Scooters Near Me Gurus
Are Doing Three Things Places That Buy Mobility Scooters Near Me
Клиент соглашается с правилами и условиями работы онлайн клуба Pokerdom,
подтверждает свое совершеннолетие.
How To Know If You’re Prepared For CS GO Case New bravo case
https://indiaph24.store/# online shopping pharmacy india
purple pharmacy mexico price list: Mexican Pharmacy Online – mexican drugstore online
Online medicine order http://indiaph24.store/# п»їlegitimate online pharmacies india
world pharmacy india
The No. Question That Everyone In Marc Jacobs Black Handbag Must Know
How To Answer marc jacobs Bag tk Maxx
mexican online pharmacies prescription drugs: mexico pharmacy – mexico drug stores pharmacies
https://canadaph24.pro/# canadianpharmacyworld