我有一个对象数组
[
{"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"}]
我需要返回[hero1,hero2,hero3]这样做的最佳方法是什么?
感谢您的帮助
答案 0 :(得分:0)
此处已经讨论了从数组中删除重复项:Unique values in an array
如果您有权使用Array.prototype.indexOf
,则可以轻松检查重复项(FF,Chrome,Opera和IE> = 9,请参阅compatibility table):
var heroes = [];
for (var i = 0 ; i < myArray.length ; i++) {
var heroName = myArray[i].hero;
if (heroes.indexOf(heroName) == -1) {
heroes.push(heroName);
}
}