如何更改嵌套数组中的值
How to change a value in nested array
代码:
let fruits = [{
name: 'apple',
attributes: [{
type: 'Granny Smith',
color: 'green',
isFavorite: true
},
{
type: 'Ambrosia',
color: 'red',
isFavorite: true
}
],
isFavorite: true
},
{
name: 'Pear',
attributes: [{
type: 'Asian',
color: 'brown',
isFavorite: true
},
{
type: 'White Pear',
color: 'white',
isFavorite: false
}
],
isFavorite: true
},
]
const fruitChecked = fruits.map(fruit => {
return { ...fruit,
isFavorite: true
}
})
const fruitAttributesChecked = fruitChecked.map(fruit => {
(fruit.attributes).map(attr => {
return { ...attr,
isFavorite: true
}
})
})
console.log(fruits)
console.log(fruitChecked)
console.log(fruitAttributesChecked)
我正在尝试将每个对象中的所有 isFavorite 值更改为 true,并将属性中的 isFavorite 值更改为 true。
但是,对于 fruitAttributesChecked
,我得到的 [undefined,undefined] 与此类似
我想要的结果如下 fruitAttributesChecked
fruitAttributesChecked =
[
{name: 'apple',
attributes: [
{type: 'Granny Smith', color:'green', isFavorite: true},
{type: 'Ambrosia', color:'red', isFavorite: true}
],
isFavorite: true
},
{name: 'Pear',
attributes: [
{type: 'Asian', color:'brown', isFavorite: true},
{type: 'White Pear', color:'white', isFavorite: false}
],
isFavorite: true
},
]
我在这里错过了什么?
您可以在对象中使用嵌套 map()
到 return 修改后的 attributes
数组。
let fruits = [{
name: 'apple',
attributes: [{
type: 'Granny Smith',
color: 'green',
isFavorite: true
},
{
type: 'Ambrosia',
color: 'red',
isFavorite: true
}
],
isFavorite: true
},
{
name: 'Pear',
attributes: [{
type: 'Asian',
color: 'brown',
isFavorite: true
},
{
type: 'White Pear',
color: 'white',
isFavorite: false
}
],
isFavorite: true
},
]
const fruitChecked = fruits.map(fruit => ({ ...fruit,
isFavorite: true,
attributes: fruit.attributes.map(attribute => ({ ...attribute,
isFavorite: true
}))
}))
console.log(fruitChecked);
const mappedFruits = fruits.map(fruit => {
return {
...fruit,
isFavorite: true,
attributes: attributes.map(attribute => {
return {
...attribute,
isFavorite: true,
}
})
}
})
代码:
let fruits = [{
name: 'apple',
attributes: [{
type: 'Granny Smith',
color: 'green',
isFavorite: true
},
{
type: 'Ambrosia',
color: 'red',
isFavorite: true
}
],
isFavorite: true
},
{
name: 'Pear',
attributes: [{
type: 'Asian',
color: 'brown',
isFavorite: true
},
{
type: 'White Pear',
color: 'white',
isFavorite: false
}
],
isFavorite: true
},
]
const fruitChecked = fruits.map(fruit => {
return { ...fruit,
isFavorite: true
}
})
const fruitAttributesChecked = fruitChecked.map(fruit => {
(fruit.attributes).map(attr => {
return { ...attr,
isFavorite: true
}
})
})
console.log(fruits)
console.log(fruitChecked)
console.log(fruitAttributesChecked)
我正在尝试将每个对象中的所有 isFavorite 值更改为 true,并将属性中的 isFavorite 值更改为 true。
但是,对于 fruitAttributesChecked
,我得到的 [undefined,undefined] 与此类似我想要的结果如下 fruitAttributesChecked
fruitAttributesChecked =
[
{name: 'apple',
attributes: [
{type: 'Granny Smith', color:'green', isFavorite: true},
{type: 'Ambrosia', color:'red', isFavorite: true}
],
isFavorite: true
},
{name: 'Pear',
attributes: [
{type: 'Asian', color:'brown', isFavorite: true},
{type: 'White Pear', color:'white', isFavorite: false}
],
isFavorite: true
},
]
我在这里错过了什么?
您可以在对象中使用嵌套 map()
到 return 修改后的 attributes
数组。
let fruits = [{
name: 'apple',
attributes: [{
type: 'Granny Smith',
color: 'green',
isFavorite: true
},
{
type: 'Ambrosia',
color: 'red',
isFavorite: true
}
],
isFavorite: true
},
{
name: 'Pear',
attributes: [{
type: 'Asian',
color: 'brown',
isFavorite: true
},
{
type: 'White Pear',
color: 'white',
isFavorite: false
}
],
isFavorite: true
},
]
const fruitChecked = fruits.map(fruit => ({ ...fruit,
isFavorite: true,
attributes: fruit.attributes.map(attribute => ({ ...attribute,
isFavorite: true
}))
}))
console.log(fruitChecked);
const mappedFruits = fruits.map(fruit => {
return {
...fruit,
isFavorite: true,
attributes: attributes.map(attribute => {
return {
...attribute,
isFavorite: true,
}
})
}
})