[英]Filtering JavaScript array of objects by property of an object and removing duplicates
I have an array of objects 我有一系列对象
[
{"title": "video01", "src":"/embed/video1","hero":"hero1"},
{"title": "video02", "src":"/embed/video2","hero":"hero1"},
{"title": "video03", "src":"/embed/video3","hero":"hero1"},
{"title": "video04", "src":"/embed/video4","hero":"hero2"},
{"title": "video05", "src":"/embed/video5","hero":"hero2"},
{"title": "video06", "src":"/embed/video6","hero":"hero3"},
{"title": "video07", "src":"/embed/video7","hero":"hero3"},
{"title": "video08", "src":"/embed/video8","hero":"hero3"}]
I need to return a [hero1, hero2, hero3] what would be the best way of doing that? 我需要返回[hero1,hero2,hero3]的最佳方法是什么?
Thanks for all your help 感谢你的帮助
Removing duplicates from an array has already been discussed here : Unique values in an array 在这里已经讨论了从数组中删除重复项:数组中的唯一值
If you have access to Array.prototype.indexOf
, checking for duplicates is easy (FF, Chrome, Opera and IE >= 9, see compatibility table ) : 如果您可以访问
Array.prototype.indexOf
,则检查重复项很容易(FF,Chrome,Opera和IE> = 9,请参见兼容性表 ):
var heroes = [];
for (var i = 0 ; i < myArray.length ; i++) {
var heroName = myArray[i].hero;
if (heroes.indexOf(heroName) == -1) {
heroes.push(heroName);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.