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://cialist.pro/# Cialis over the counter
See What Online Shopping Figures Uk Tricks The Celebs Are
Making Use Of online shopping Figures uk [http://49.0.65.75/]
Are You Responsible For The Double Glazing Repair Near Me Budget?
10 Amazing Ways To Spend Your Money double Glazing Near me
What The 10 Most Worst Freezer FAILURES Of All Time Could Have Been Prevented Energy Star freezers
Where Will Malpractice Litigation Be One Year From Today?
pilot point malpractice attorney (https://vimeo.Com)
http://viagras.online/# generic sildenafil
Ask Me Anything: 10 Answers To Your Questions
About Best American Style Fridge Freezer american fridge freezer with ice dispenser
10 Best Facebook Pages That I’ve Ever Seen. Electric Wall.Mounted Fire
Wall Mounted Electric Fires (Minecraftathome.Com)
Viagra online price Buy Viagra online cheap viagra
I am genuinely thankful to the holder of this web page who has shared this fantastic article at at this time.
Speak “Yes” To These 5 Window Companies Leeds Tips glaziers
What’s The Job Market For Erb’s Palsy Compensation Professionals Like?
Palsy
The Main Issue With Veterans Disability Attorneys, And How You Can Repair It Martins Ferry Veterans Disability Lawyer
Locksmiths Near Me For Car: 10 Things I’d Like
To Have Known In The Past mobile locksmith for cars near me (Nichol)
10 Things That Your Family Taught You About Designer Handbags White
designer handbags white (Eli)
Are You Getting The Most Of Your Designer Bags?
cheap designer Bags
5 Cliches About Injury Attorneys You Should Avoid Vimeo
10 Startups That’ll Change The Waitrose Groceries Online Shopping Uk Industry For The Better Woodworking Pneumatic Nailer
Generic Levitra 20mg: buy Levitra over the counter – Vardenafil buy online
The 10 Most Terrifying Things About Volkswagen Replacement Key Cost Uk volkswagen replacement key cost uk
Think You’re The Perfect Candidate For Doing Shopping Online?
Do This Test High-Performance Break Cue
20 Fun Infographics About Online Shopping Sites For Clothes famous
online shopping sites for clothes; https://jtbtigers.com,
What Are The Biggest “Myths” Concerning Remote Key Fob Repair Could
Actually Be True car key fob repair near me; Bridget,
Ten Mobile Car Locksmiths That Really Help You Live Better Mobile Car Locksmiths
Why You Should Focus On Enhancing Double Glazed Repairs Near
Me Misted Double Glazing
15 Best 5kw Multi Fuel Stove Bloggers You Need To Follow best 5kw Multi fuel stove
[slimex365.com]
The Most Worst Nightmare About Private ADHD Assessment UK Be Realized Private Adult adhd Assessment
buy cenforce cenforce for sale Buy Cenforce 100mg Online
Guide To Truck Accident Attorney Near Me: The Intermediate Guide In Truck Accident Attorney Near
Me truck accident attorney near me (Brodie)
This post presents clear idea in favor of the new
viewers of blogging, that genuinely how to do blogging and site-building.
My web-site zapakita01