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
get generic cefixime no prescription
porn
I am in fact thankful to the owner of this site who has shared this impressive
piece of writing at at this time.
Here is my webpage :: asbestos abatement ottawa
I read this article fully about the resemblance of most up-to-date and earlier technologies,
it’s remarkable article.
Here is my web blog :: asbestos
Tips To Leave Of Your Card Debt Now! 중소기업 대출
does doxycycline kill gut bacteria
How To Choose The Right Double Glazed Front Doors Near Me On The Internet double Glased windows
buying prescription drugs in mexico online
https://cmqpharma.com/# mexico drug stores pharmacies
mexican border pharmacies shipping to usa
incest porn
buying prescription drugs in mexico
http://cmqpharma.com/# mexico drug stores pharmacies
mexico pharmacies prescription drugs
Good article. I absolutely appreciate this website.
Stick with it!
medication from mexico pharmacy: mexican online pharmacy – medicine in mexico pharmacies
Relief from pain and swelling in joints
Be On The Lookout For: How Medical Malpractice Litigation Is Taking Over And What Can We Do About It medical malpractice attorneys
You’ll Never Guess This Double Glazed Replacement Windows’s
Benefits Double Glazed Replacement Windows
Piece of writing writing is also a fun, if you be acquainted with then you can write or else it is complicated to write.
buy cheap doxycycline no prescription
mexican mail order pharmacies
http://cmqpharma.com/# п»їbest mexican online pharmacies
reputable mexican pharmacies online
best online pharmacies in mexico: mexican online pharmacy – mexican rx online
https://cyprusrent.ru/
can i get cabgolin without prescription
medication from mexico pharmacy
https://cmqpharma.online/# mexican mail order pharmacies
best online pharmacies in mexico
Bad Credit Bank Account 보증금 대출 (f.r.a.g.ra.nc.e.rnmn)
buying prescription drugs in mexico online: cmqpharma.com – mexico pharmacy
mexico drug stores pharmacies: online mexican pharmacy – best online pharmacies in mexico
buying prescription drugs in mexico online
https://cmqpharma.com/# mexican online pharmacies prescription drugs
mexico drug stores pharmacies
Amazing blog! Do you have any hints for aspiring writers?
I’m hoping to start my own website soon but I’m a little lost on everything.
Would you suggest starting with a free platform like WordPress or go for a paid option? There are so many
choices out there that I’m totally confused .. Any recommendations?
Many thanks!
mexican drugstore online
https://cmqpharma.online/# medicine in mexico pharmacies
mexican rx online
mexico pharmacies prescription drugs: cmqpharma.com – pharmacies in mexico that ship to usa
What’s up to all, how is everything, I think every one is getting more from this website,
and your views are nice for new visitors.
Here is my web-site … asbestos abatement ottawa