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
You’re About To Expand Your Windows And Doors Leeds Options repair double Glazing (https://telegra.ph/)
I’m not sure exactly why but this website is loading extremely slow for me.
Is anyone else having this issue or is it a problem on my end?
I’ll check back later on and see if the problem still exists.
5 Clarifications On Car Accident Lawyers Automobile
https://viagras.online/# Viagra online price
The 9 Things Your Parents Teach You About Upvc Window Repairs upvc window Repairs
What’s The Current Job Market For Double Glazing Near Me Professionals Like?
double glazing near me
A Provocative Remark About Washers Dryer Combo Washer and Dryer machine Combo
You’ll Be Unable To Guess Double Glazing Repair Leeds’s
Tricks Double glazing repair leeds – https://www.dermandar.com/user/dangerlisa1 –
The Full Guide To Childrens Bunk Beds bunk beds uk
Watch Out: How cheapest beko washing machine; Darwin, Washing Is Gaining Ground And What To Do
About It
I like the helpful information you provide in your
articles. I will bookmark your blog and check again here regularly.
I am quite sure I’ll learn lots of new stuff right here!
Best of luck for the next!
These are so-known as identical day payout loans.
Nicely put. Appreciate it.
15 Things Your Boss Would Like You To Know You’d Known About Double Glazing Repairs Near Me window repairs
12 Stats About Bmw Replacement Key Cost To Make You Take A Look At Other People bmw replacement key Cost uk
http://viagras.online/# Order Viagra 50 mg online
cheapest cialis: cialist.pro – Buy Tadalafil 5mg
https://levitrav.store/# Levitra generic best price
7 Helpful Tips To Make The The Most Of Your Window Repair Leeds window hinge Repair leeds (funsilo.Date)
This Is The Myths And Facts Behind Medical Malpractice Lawyer haltom city medical malpractice lawsuit
You’ll Never Be Able To Figure Out This Tommy Hilfiger Bags For Ladies’s Secrets Designer Bags tommy hilfiger
15 Terms Everybody Is In The Veterans Disability Compensation Industry Should Know Vimeo
Hello there! I could have sworn I’ve visited your blog before
but after going through a few of the articles I realized it’s new to me.
Nonetheless, I’m definitely delighted I stumbled upon it and
I’ll be bookmarking it and checking back frequently!
Is Tech Making Birth Injury Law Better Or Worse?
Heber birth injury lawyer
How To Find The Perfect Windows And Doors Leeds Online Upvc Window Handle Repairs Leeds
You’ll Never Be Able To Figure Out This Small Multi Fuel Stove’s Tricks small
multi fuel stove – Shannan,
10 Best Facebook Pages Of All Time About Avon Leadership Team
form
A Peek In The Secrets Of Window Repair Near upvc window repair near me; Madonna,
5 Laws Everyone Working In Realisticsex Doll
Should Be Aware Of TOPS Adult Toys
Solutions To Issues With London Window And Door south
london Windows (elearnportal.Science)