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
This Week’s Top Stories Concerning Amazon Online Shopping Clothes Uk
Vimeo.Com
Why We Enjoy Railroad Injuries Attorney (And
You Should Also!) Vimeo
Solutions To Problems With Fireplaces Wood Burning Stoves Fireplaces And Stove
10 Things That Your Family Teach You About Nespresso
Coffee Machine Magimix nespresso coffee machine Magimix (https://buhl-andersson-2.mdwrite.net)
You’ll Never Be Able To Figure Out This Double Sided
Multi Fuel Stove’s Secrets double Sided multi fuel stove (https://yoosure.com/go8/index.php?goto=https://Www.fireplacesandstove.com/products/nrg-5kw-eco-stove-multifuel-cast-iron-351)
14 Questions You Might Be Uneasy To Ask CSGO Cases To Invest
In Prisma 2 Case
Извините, я удалил эту мысль 🙂
Разработка Сайтов разработка.site – это креативный процесс, требующий знаний, мастерства и опыта. Профессионалы выявляют желания заказчика и превращают их в функциональные интернет-страницы.
It Is Also A Guide To Lost Car Keys No Spare In 2023 lost key to car no spare (Dirk)
15 Unexpected Facts About Online Home Based Work That You Never Known Work From Home No Experience
What Is The Best Folding Mobility Scooter To Buy Techniques To Simplify Your Daily Lifethe One What Is The Best Folding
Mobility Scooter To Buy Trick That Should Be Used By Everyone Learn Best Folding Mobility Scooter
mexican drugstore online mexico pharmacies prescription drugs purple pharmacy mexico price list
Five Killer Quora Answers To Situs Alternatif Gotogel Situs Alternatif Gotogel
5 Killer Quora Answers To Double Glazing Repairs Leeds double Glazing repairs leeds
What’s The Job Market For Replacement Window Glass Near Me Professionals?
Replacement Window Glass
mexican border pharmacies shipping to usa mexican pharmaceuticals online mexican border pharmacies shipping to usa
15 Amazing Facts About CSGO Cases Opening You’ve Never Known Gloves Cases
Online Shopping Sites List For Clothes Tools To Ease Your Daily Life Online Shopping Sites List For Clothes Technique Every Person Needs To Know Online Shopping Sites List For Clothes
The 10 Scariest Things About Espresso Maker espresso coffee machine home
cheapest pharmacy for prescription drugs pharm world store canadian pharmacy coupon
online pharmacy non prescription drugs: pharm world – canadian pharmacy coupon code
online meds no prescription canadian prescription drugstore reviews prescription canada
world pharmacy india: cheapest online pharmacy india – world pharmacy india
Five Killer Quora Answers On Double Glazed Window
Repairs Near Me Double Glazed Window Repairs Near Me
Attractive section of content. I just stumbled upon your site and in accession capital to
assert that I acquire in fact enjoyed account your blog
posts. Anyway I will be subscribing to your augment and
even I achievement you access consistently rapidly.
You’ll Never Guess This Double Glaze Repair Near Me’s Secrets Double Glaze Repair Near Me
Nine Things That Your Parent Taught You About ADHD Diagnosis UK Adults adhd
diagnosis Uk adults, trademarketclassifieds.Com,
medication from mexico pharmacy: reputable mexican pharmacies online – mexican rx online
Ten Common Misconceptions About Private ADHD Assessment Glasgow That Don’t Always Hold adult adhd assessment Private
https://pharmworld.store/# canada drugs coupon code
The No. Question Everybody Working In Window Repairs Luton Should Be Able
To Answer services