Hey,
I can’t figure out how
obj[user.name] = user.age;
from the callback function is working there. The =
sign is an assignment, shouldn’t we have a +=
operator to add the value of user.age
to obj[user.name]
instead ?
Sorry in advance if you find my question silly, but it’s important for me to understand each steps of the program.
Thank you!
const users = [
{ name: 'John', age: 34 },
{ name: 'Amy', age: 20 },
{ name: 'camperCat', age: 10 }
];
const usersObj = users.reduce((obj, user) => {
obj[user.name] = user.age;
return obj;
}, {});
console.log(usersObj);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.41 Safari/537.36
Challenge: Use the reduce Method to Analyze Data
Link to the challenge: