为什么我的map()与spread操作符不起作用?

我真的没有看到这是错的地方。我从O'Reilly的Learning React中看到了这个特殊例子的帖子,银行& Porcello。然而,这些帖子似乎工作正常,但我的例子没有。如果我有一个错字,我不会看到它。我的缺点在哪里?我不知道为什么我得到一个空字符串值,而不是为什么我的map()与spread操作符不起作用?

<!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/[email protected]/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>

回答:

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"

您没有添加的名称的新值“HB伍德朗”,而不是离开它空。新增name:newName

以上是 为什么我的map()与spread操作符不起作用? 的全部内容, 来源链接: utcz.com/qa/262193.html

回到顶部