使用 ES6 中的 map 函数更新对象的属性值
Update the attribute value of an object using the map function in ES6
我正在尝试用 ES6 编写代码。以下是我想要实现的目标。假设我有一个名为 schools
.
的对象数组
let schools = [
{name: 'YorkTown', country: 'Spain'},
{name: 'Stanford', country: 'USA'},
{name: 'Gymnasium Achern', country: 'Germany'}
];
现在,我想编写一个名为 editSchoolName
的函数,它将采用 3 个参数,schools
(这是我在上面定义的数组)、oldName
和 name
.
我将在参数 oldName
中传递学校名称,并且该名称应更新为参数 name
中的值。
我不想更改变量的状态 schools
,所以我使用 map
函数,它将 return 一个包含更改的新数组。
editSchoolName
函数会这样调用-
var updatedSchools = editSchoolName(schools, "YorkTown", "New Gen");
这里,名字YorkTown
应该替换成名字New Gen
。所以数组updatedSchools
的期望值应该是-
let updatedSchools = [
{name: 'New Gen', country: 'Spain'},
{name: 'Stanford', country: 'USA'},
{name: 'Gymnasium Achern', country: 'Germany'}
];
这就是我的 editSchoolName 函数的样子 -
const editSchoolName = (schools, oldName, name) =>
schools.map(item => {
if (item.name === oldName) {
/* This is the part where I need the logic */
} else {
return item;
}
});
需要帮助更改 editSchoolName
函数以实现上述预期结果。
您需要return更新对象:
const editSchoolName = (schools, oldName, name) =>
schools.map(item => {
if (item.name === oldName) {
return {...item, name};
} else {
return item;
}
});
const editSchoolName = (schools, oldName, newName) =>
schools.map(({name, ...school }) => ({ ...school, name: oldName === name ? newName : name }));
你可以使用三元来缩短它。
试试这个,ES6 Object.assign()
创建数组元素的副本并更新新对象。
let schools = [{
name: 'YorkTown',
country: 'Spain'
},
{
name: 'Stanford',
country: 'USA'
},
{
name: 'Gymnasium Achern',
country: 'Germany'
}
];
const editSchoolName = (schools, oldName, name) => {
return schools.map(item => {
var temp = Object.assign({}, item);
if (temp.name === oldName) {
temp.name = name;
}
return temp;
});
}
var updatedSchools = editSchoolName(schools, "YorkTown", "New Gen");
console.log(updatedSchools);
console.log(schools);
使用解构
const schools = [
{
name: "YorkTown",
country: "Spain",
},
{
name: "Stanford",
country: "USA",
},
{
name: "Gymnasium Achern",
country: "Germany",
},
];
const editSchoolName = (schools, oldName, newName) =>
schools.map(({ name, ...school }) => ({
...school,
name: oldName === name ? newName : name,
}));
const updatedSchools = editSchoolName(schools, "YorkTown", "New Gen");
console.log(updatedSchools);
let schools = [{
name: 'YorkTown',
country: 'Spain'
},
{
name: 'Stanford',
country: 'USA'
},
{
name: 'Gymnasium Achern',
country: 'Germany'
}
];
let updatedSchools = [{
name: 'New Gen',
country: 'Spain'
},
{
name: 'Stanford',
country: 'USA'
},
{
name: 'Gymnasium Achern',
country: 'Germany'
}
];
const editSchoolName = ((schools, oldName, name) =>{
schools.map(item => {
if (item.name === oldName) {
item.name = name;
return item.name;
} else {
return item;
}
});
console.log(schools);
});
editSchoolName(schools, 'YorkTown', "New Gen");
如果您只想编辑评论部分:
const editSchoolName = (schools, oldName, name) =>
schools.map(item => {
if (item.name === oldName) {
var newItem = Object.assign({},item);
newItem.name = name;
return newItem;
}
else{
return item;
}
});
我想知道为什么 none 的答案给出了简单的解决方案
const editSchoolName = (schools, oldName, newName) =>
schools.map(school => { if (school.name === oldName) school.name = newName;
return school;
});
就这么简单:
const editSchoolName = ((schools, oldName, name) =>{
let results =schools.map((item,index) => {
if (item.name === oldName) {
let newItem = {...item, name}
return newItem;
} else {
return item;
}
});
return results;
});
我正在尝试用 ES6 编写代码。以下是我想要实现的目标。假设我有一个名为 schools
.
let schools = [
{name: 'YorkTown', country: 'Spain'},
{name: 'Stanford', country: 'USA'},
{name: 'Gymnasium Achern', country: 'Germany'}
];
现在,我想编写一个名为 editSchoolName
的函数,它将采用 3 个参数,schools
(这是我在上面定义的数组)、oldName
和 name
.
我将在参数 oldName
中传递学校名称,并且该名称应更新为参数 name
中的值。
我不想更改变量的状态 schools
,所以我使用 map
函数,它将 return 一个包含更改的新数组。
editSchoolName
函数会这样调用-
var updatedSchools = editSchoolName(schools, "YorkTown", "New Gen");
这里,名字YorkTown
应该替换成名字New Gen
。所以数组updatedSchools
的期望值应该是-
let updatedSchools = [
{name: 'New Gen', country: 'Spain'},
{name: 'Stanford', country: 'USA'},
{name: 'Gymnasium Achern', country: 'Germany'}
];
这就是我的 editSchoolName 函数的样子 -
const editSchoolName = (schools, oldName, name) =>
schools.map(item => {
if (item.name === oldName) {
/* This is the part where I need the logic */
} else {
return item;
}
});
需要帮助更改 editSchoolName
函数以实现上述预期结果。
您需要return更新对象:
const editSchoolName = (schools, oldName, name) =>
schools.map(item => {
if (item.name === oldName) {
return {...item, name};
} else {
return item;
}
});
const editSchoolName = (schools, oldName, newName) =>
schools.map(({name, ...school }) => ({ ...school, name: oldName === name ? newName : name }));
你可以使用三元来缩短它。
试试这个,ES6 Object.assign()
创建数组元素的副本并更新新对象。
let schools = [{
name: 'YorkTown',
country: 'Spain'
},
{
name: 'Stanford',
country: 'USA'
},
{
name: 'Gymnasium Achern',
country: 'Germany'
}
];
const editSchoolName = (schools, oldName, name) => {
return schools.map(item => {
var temp = Object.assign({}, item);
if (temp.name === oldName) {
temp.name = name;
}
return temp;
});
}
var updatedSchools = editSchoolName(schools, "YorkTown", "New Gen");
console.log(updatedSchools);
console.log(schools);
使用解构
const schools = [
{
name: "YorkTown",
country: "Spain",
},
{
name: "Stanford",
country: "USA",
},
{
name: "Gymnasium Achern",
country: "Germany",
},
];
const editSchoolName = (schools, oldName, newName) =>
schools.map(({ name, ...school }) => ({
...school,
name: oldName === name ? newName : name,
}));
const updatedSchools = editSchoolName(schools, "YorkTown", "New Gen");
console.log(updatedSchools);
let schools = [{
name: 'YorkTown',
country: 'Spain'
},
{
name: 'Stanford',
country: 'USA'
},
{
name: 'Gymnasium Achern',
country: 'Germany'
}
];
let updatedSchools = [{
name: 'New Gen',
country: 'Spain'
},
{
name: 'Stanford',
country: 'USA'
},
{
name: 'Gymnasium Achern',
country: 'Germany'
}
];
const editSchoolName = ((schools, oldName, name) =>{
schools.map(item => {
if (item.name === oldName) {
item.name = name;
return item.name;
} else {
return item;
}
});
console.log(schools);
});
editSchoolName(schools, 'YorkTown', "New Gen");
如果您只想编辑评论部分:
const editSchoolName = (schools, oldName, name) =>
schools.map(item => {
if (item.name === oldName) {
var newItem = Object.assign({},item);
newItem.name = name;
return newItem;
}
else{
return item;
}
});
我想知道为什么 none 的答案给出了简单的解决方案
const editSchoolName = (schools, oldName, newName) =>
schools.map(school => { if (school.name === oldName) school.name = newName;
return school;
});
就这么简单:
const editSchoolName = ((schools, oldName, name) =>{
let results =schools.map((item,index) => {
if (item.name === oldName) {
let newItem = {...item, name}
return newItem;
} else {
return item;
}
});
return results;
});