I have a pair of arrays consisting of objects:
var books = [
{ id: 1, name: 'Book X' },
{ id: 2, name: 'Book Y' }
];
var cars = [
{ id: 1, name: 'Car P' },
{ id: 2, name: 'Car Q' },
{ id: 3, name: 'Car R' },
];
My objective is to generate an array of strings that includes:
1. The title of the first Book in the books array (if available)
2. The titles of the initial 2 Cars in the cars array (if available)
I can achieve this by:
if (books.length > 0)
var firstBook = books[0].name;
Alternatively:
if (cars.length > 1) {
var firstCar = cars[0].name;
var secondCar = cars[1].name;
}
I am open to suggestions for better approaches to accomplish this task.