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
Post a Comment