Is it possible to merge two arrays in Angular's ng-repeat functionality?
For instance, let's say we have arrays containing book titles and author names. How can we display the book titles along with their corresponding author names?
One array holds the book data, while another contains the author information.
What is the best way to combine the data in this scenario?
Check out the JSFiddle example: http://jsfiddle.net/1jxsh0x3/
var myApp = angular.module('myApp', []);
function MyCtrl($scope) {
$scope.authors = [{
id: "1",
name: "John"
}, {
id: "2",
name: "Peter"
}, {
id: "3",
name: "Mark"
}];
$scope.books = [{
id: "1",
id_author: "3",
name: "Book Lorem"
}, {
id: "2",
id_author: "1",
name: "Book Ipsum"
}, {
id: "3",
id_author: "1",
name: "Book Dark"
}];
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
<div ng-controller="MyCtrl">
<div ng-repeat="book in books">
<div>Book title: {{book.name}}</div>
<div>Authors name: {{authors[$index].name}}</div>
<hr>
</div>
</div>