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?
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 ) :
var heroes = [];
for (var i = 0 ; i < myArray.length ; i++) {
var heroName = myArray[i].hero;
if (heroes.indexOf(heroName) == -1) {
heroes.push(heroName);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.