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
Guide To Fiat Punto Key Replacement: The Intermediate Guide
Towards Fiat Punto Key Replacement fiat punto key Replacement
8 Tips To Improve Your Saab Replacement Key Game replaced
cheap canadian pharmacy: canadian pharmacy india – canadian pharmacy ratings
buy prescription drugs from india indian pharmacies safe reputable indian online pharmacy
canadian prescription drugstore review: order medication without prescription – quality prescription drugs canada
10 Unexpected Tier 2 Links Seo Tips tier backlinks (Declan)
best online pharmacies in mexico: medication from mexico pharmacy – mexican pharmaceuticals online
Everything is very open with a really clear explanation of the challenges.
It was definitely informative. Your site is useful. Thank you for sharing!
my blog post: assessment development center
and yet, take a look at this scene now, inside movie, but not
at the hype, as in analsex.tube, and it will
appear to you not excessively successful.
12 Facts About Porsche Spare Key To Make You Take A Look At Other People porsche key Replacement
You’ll Be Unable To Guess Birth Defect Settlement’s Benefits birth defect
Советую Вам посетить сайт, на котором есть много статей по этому вопросу.
Разработка Сайтов разработка.site – это творческий процесс, требующий умений, мастерства и опыта. Специалисты определяют потребности заказчика и превращают их в функциональные веб-сайты.
mail order pharmacy india: india online pharmacy – mail order pharmacy india
5 Motives Best Case Opening Site CSGO Can Be A Beneficial Thing Case revolution
20 Resources To Make You More Effective At Railroad Injuries Attorneys
Firm
purple pharmacy mexico price list mexican pharmacy п»їbest mexican online pharmacies
Let’s Get It Out Of The Way! 15 Things About Veterans Disability Lawyer We’re Tired
Of Hearing Sweetwater veterans disability attorney
The Reason Why Double Glazing Door Repairs Near Me Is The Most-Wanted Item In 2023 Repairs
5 Killer Quora Answers On Window Repairs Near Me window repairs near me (promarket.In.ua)
Seven Explanations On Why Adhd In Adults Treatment Is Important
treating adult adhd
foreign pharmacy no prescription cheapest pharmacy promo code for canadian pharmacy meds
What Do You Do To Know If You’re Prepared To Go After Diagnostics Automotive how much are Car diagnostic tests – lzdsxxb.com,
Online medicine home delivery: top 10 online pharmacy in india – indian pharmacy
top online pharmacy india Online medicine home delivery Online medicine order
The 10 Scariest Things About Cheapest Beko Washing Machine cheapest Beko Washing machines
12 Companies Leading The Way In Car Key Program Near Me reprogramming
https://pharmmexico.online/# mexican online pharmacies prescription drugs
https://pharmcanada.shop/# safe canadian pharmacy
legit canadian online pharmacy ordering drugs from canada cheap canadian pharmacy
no prescription needed: pharmacy with no prescription – buying drugs online no prescription