angularjs - Angular merge nested arrays -


i have dataset looks this:

[   {     'title' : 'cats',     'names' : [       'felix',       'tom',       ... more names     ]   },   {     'title' : 'dogs',     'names' : [       'fido',       'rover',       ... more names     ]   },   {     ... more animal types ] 

and have following:

<p ng-repeat='name in names'>{{ name }}</p> 

but, need @ stage set

$scope.names = ['felix', 'tom', 'fido', rover']; 

my question is: there 'angular' way merge arrays or take content multiple places 1 object? or need use loop concat function create array use?

sure, defined names based on data, demo.

$scope.names = function() {   return array.prototype.concat.apply([], animals.map(function(animal) {     return animal.names;   })); }; 

then use method in view

<p ng-repeat='name in names()'>{{ name }}</p> 

or assume list of animals won't change, , use library lodash readability, demo.

$scope.names = _.chain(animals)   .pluck('names')   .flatten()   .value() 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -