JavaScript JavaScript Array Iteration Methods Combining Array Methods Working with Objects

victor escarcega
victor escarcega
8,667 Points

Working with map

not sure what I am missing, the error says I am missing ) after argument,

app.js
const authors = [
  { firstName: "Beatrix", lastName: "Potter" },
  { firstName: "Ann", lastName: "Martin" },
  { firstName: "Beverly", lastName: "Cleary" },
  { firstName: "Roald", lastName: "Dahl" },
  { firstName: "Lewis", lastName: "Carroll" }
];
let fullAuthorNames;
fullAuthorNames = authors.map(i => {
  i = `${authors.firstName}, ${authors.lastName}`;
  return i;
};)



// fullAuthorNames should be: ["Beatrix Potter", "Ann Martin", "Beverly Cleary", "Roald Dahl", "Lewis Carroll"]
// Write your code below

1 Answer

imo
imo
7,892 Points
// Pretty close, but there are a 3 issues with your code
// You can't use authors to get firstName and lastName, because authors returns the whole key value array
// and now the individual block
i = `${authors.firstName}, ${authors.lastName}`;

// If you look at your code here, you can use the ' i ' to retrieve the individual block
// which would look like this: { firstName: "Beatrix", lastName: "Potter" },
// now with this block you can use .firstName and .lastName
fullAuthorNames = authors.map(i => {

// Code corrected
// You don't need the ' ,  ' here
${i.firstName}, ${i.lastName}

// move ' ) ' before ' ; '
};)
// like this
});

// Also you don't need to use i =  `${i.firstName}, ${i.lastName}`
// you can just
return `${i.firstName} ${i.lastName}`

// Final look
fullAuthorNames = authors.map(i=> {
  return `${i.firstName} ${i.lastName}`;
});
victor escarcega
victor escarcega
8,667 Points

Super helpful thank you Senpai!

Steven Parker
Steven Parker
207,988 Points

This would also be a good place to use the compact form of an arrow function:

fullAuthorNames = authors.map(i => `${i.firstName} ${i.lastName}`);