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
Hi there, after reading this remarkable article i am too glad to share my familiarity here with friends.
Seven Explanations On Why Renault Kadjar Replacement
Key Is So Important renault kangoo replacement key (Tonja)
buy prescription drugs online cheap drugs over the counter ed medication
ed vacuum pump cheap pharmacy online male erection pills
india pharmacy mail order Order medicine from India to USA indian pharmacy online
Ridiculous quest there. What occurred after?
Take care!
The Three Greatest Moments In Situs Togel Dan Slot Terpercaya History Situs Togel Terpercaya
Официальная покупка диплома вуза с сокращенной программой в Москве
online drug store buy drugs ed pills online
п»їlegitimate online pharmacies india Indian pharmacy online п»їlegitimate online pharmacies india
Why You Should Not Think About How To Improve Your Pushchair 2 In 1
2 In 1 Pram Sale
5 Myths About Anxiety Disorder That You Should Stay Clear Of Anxiety disorders Phobia (glamorouslengths.com)
Good day! This is kind of off topic but I need some help from
an established blog. Is it tough to set up your own blog?
I’m not very techincal but I can figure things out pretty quick.
I’m thinking about creating my own but I’m not sure where to begin.
Do you have any points or suggestions? Thank you
over the counter ed: cheap pharmacy online – cheap medication
http://pharm24.pro/# drug medication
psychological ed treatment
Сколько стоит диплом высшего и среднего образования и как его получить?
medicine in mexico pharmacies mexican drugs pharmacies in mexico that ship to usa
Официальная покупка диплома вуза с сокращенной программой обучения в Москве
I love your blog.. very nice colors & theme. Did you make this
website yourself or did you hire someone to do it for
you? Plz respond as I’m looking to create my own blog and would like to find out where u got this from.
appreciate it
buy generic ed pills online: cheap prescription drugs – erectile dysfunction
Hi there, You have done an excellent job. I’ll definitely digg it
and personally suggest to my friends. I’m sure they’ll
be benefited from this website.
5 Clarifications Regarding Espresso Machine best coffee espresso Machine
Ultra Realistic Sex Doll Tools To Ease Your Daily Lifethe One Ultra Realistic Sex Doll Trick Every Individual Should Know Ultra Realistic Sex Doll
10 Wrong Answers To Common Accident Lawyers Gainesville Questions:
Do You Know Which Answers? accident Injury
The Top Fridge Freezer Bosch Gurus Can Do Three
Things Fridge Freezer Collection
The History Of Bean-To-Cup Coffee Machines Coffee Machines That Use Beans
п»їbest mexican online pharmacies: mexican pharm 24 – pharmacies in mexico that ship to usa
5 Conspiracy Theories About Signs And Symptoms
Of ADHD In Women You Should Stay Clear Of adhd assessment for adult Women
can you get cheap ventolin inhalator price
7 Simple Secrets To Totally Refreshing Your Hyundai Key Fob Hyundai Key Remote Replacement