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
Thanks for sharing your thoughts on website design billings.
Regards
1. Как выбрать идеальный гипсокартон для ремонта
магазины строительных материалов москва купить гипсокартон .
canadian pharmacy 24 Canada pharmacy canadian pharmacy
penis enlargement
If you are going for finest contents like me, only
pay a visit this site all the time for the reason that it presents quality contents, thanks
https://canadianpharmgrx.xyz/# canadian pharmacy world
canadian pharmacy online reviews Canada pharmacy online canadianpharmacy com
canadian pharmacy tampa: Canada pharmacy online – canada pharmacy
where to buy cheap imuran price
Hey There. I found your blog using msn. This is an extremely well written article.
I will make sure to bookmark it and return to read more of your useful information.
Thanks for the post. I’ll definitely comeback.
my webpage design website billings
I am curious to find out what blog platform you have been utilizing?
I’m experiencing some minor security issues with my
latest blog and I’d like to find something more safe.
Do you have any recommendations?
It’s perfect time to make some plans for the future and it’s time to
be happy. I’ve read this post and if I could I desire to suggest you few interesting
things or suggestions. Perhaps you could write next articles referring to this article.
I desire to read more things about it!
legitimate canadian pharmacy CIPA approved pharmacies northwest pharmacy canada
medication from mexico pharmacy: Pills from Mexican Pharmacy – mexican drugstore online
mexico drug stores pharmacies: Pills from Mexican Pharmacy – medicine in mexico pharmacies
https://indianpharmgrx.com/# reputable indian pharmacies
fioredivino.ru
buying prescription drugs in mexico mexican pharmacy medicine in mexico pharmacies
mexico drug stores pharmacies Pills from Mexican Pharmacy mexican online pharmacies prescription drugs
Here is my web-site – kkpoker review (cadplm.co.kr)
mexican mail order pharmacies Pills from Mexican Pharmacy mexican online pharmacies prescription drugs
http://canadianpharmgrx.com/# buy drugs from canada
online pharmacy india indian pharmacy п»їlegitimate online pharmacies india
Touche. Outstanding arguments. Keep up the great work.
My webpage :: design website billings
best online pharmacy india: indian pharmacy delivery – п»їlegitimate online pharmacies india
Hey there, You’ve done a fantastic job. I will certainly digg it and personally recommend
to my friends. I am confident they will be benefited from this web design billings
site.
Отличный сайт! Всем рекомендую, качественный и быстрый ремонт техники! ремонт iphone спб
My developer is trying to convince me to move to .net from PHP.
I have always disliked the idea because of the costs. But he’s tryiong none the less.
I’ve been using WordPress on a variety of websites for about a year and am worried about switching to another platform.
I have heard fantastic things about blogengine.net. Is there a way I can transfer
all my wordpress posts into it? Any kind of help would be really appreciated!
Какой призовой https://gagdetfrontal.com/ фонд покер фриролла?
is canadian pharmacy legit: canadian drug pharmacy – canada pharmacy world