I am currently running npm test on the object books. My objective is to retrieve the value of the title property from the books object. When I invoke the function getTheTitles(books), both 'Book' and 'Book2' should be returned. However, upon checking the result after executing npm test, I noticed that there was an additional undefined value alongside Book and Book2. Do I need to make any adjustments in my getTheTiles function? The code snippet is provided below:
const books = [
{
title: 'Book',
author: 'Name'
},
{
title: 'Book2',
author: 'Name2'
}
]
let titles = ['title'];
const getTheTitles = function(item) {
return titles.map(function(k) {
return item[k];
})
}
getTheTitles(books);
--------------- Below includes the npm test along with its expected outcome ---------------
const getTheTitles = require('./getTheTitles')
describe('getTheTitles', () => {
const books = [
{
title: 'Book',
author: 'Name'
},
{
title: 'Book2',
author: 'Name2'
}
]
test('gets titles', () => {
expect(getTheTitles(books)).toEqual(['Book','Book2']);
});
});