Why is my map() with spread syntax not working?

Why is my map() with spread syntax not working?



I'm really not seeing where this is going wrong. I've seen posts of this particular example from O'Reilly's Learning React, by Banks & Porcello. However, the posts seem to work fine, but my example does not. If I have a typo, I don't see it. Where's my flaw? I don't know why I get a null string value instead of "HB Woodlawn"


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
</head>
<body>

<script type="text/babel">

// Editing one object in an array of objects

let schools = [
name: 'Yorktown',
name: 'Stratford',
name: 'Washington & Lee',
name: 'Wakefield'
];

const editName = (oldName, newName, arr) =>
arr.map(item =>
if (item.name === oldName)
return
...item,
name


else
return item

);

let updatedSchools = editName('Stratford', 'HB Woodlawn', schools);

console.log(updatedSchools[1]); // name: ""
console.log(schools[1]); // name: "Stratford"

</script>

</body>
</html>






Note: ... is not an operator. (And before anyone says it: Ignore the URL on the MDN page, it was an error by the person who created the page.)

– T.J. Crowder
Dec 18 '17 at 9:52


...






ummm, ... is spread operator right?

– illiteratewriter
Dec 18 '17 at 9:57


...




1 Answer
1


let schools = [
name: 'Yorktown',
name: 'Stratford',
name: 'Washington & Lee',
name: 'Wakefield'
];

const editName = (oldName, newName, arr) =>
arr.map(item =>
if (item.name === oldName)
return
...item,
name: newName


else
return item

);

let updatedSchools = editName('Stratford', 'HB Woodlawn', schools);

console.log(updatedSchools[1]); // name: ""
console.log(schools[1]); // name: "Stratford"



You hadn't added the new value for the name, instead had left it empty. Added name:newName


name:newName






If you liked the answer click on the tick mark. Thank you.

– illiteratewriter
Dec 18 '17 at 11:21



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.