admin管理员组文章数量:1410730
Code:
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)
Code:
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)
I am trying to change ALL the isFavorite value to true within each object and the isFavorite value within attributes to true.
However I'm getting something similar to this for [undefined,undefined] for fruitAttributesChecked
The oute I desire is below for 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
},
]
What am I missing here?
Share Improve this question edited Aug 18, 2021 at 23:02 Barmar 784k57 gold badges548 silver badges659 bronze badges asked Aug 18, 2021 at 22:56 Cartileo Cartileo 31 silver badge4 bronze badges 3-
2
You're not returning the value of
(fruit.attributes).map
– Barmar Commented Aug 18, 2021 at 23:03 - Sorry new to this. That makes sense, I have to return ``` (fruit.attributes).map(attr => { return { ...attr, isFavorite: true } ``` – Cartileo Commented Aug 18, 2021 at 23:04
- @Robson thats a typo. the desired should have all true – Cartileo Commented Aug 18, 2021 at 23:05
2 Answers
Reset to default 4You can use a nested map()
to return a modified attributes
array in the object.
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,
}
})
}
})
本文标签: javascriptHow to change a value in nested arrayStack Overflow
版权声明:本文标题:javascript - How to change a value in nested array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744926331a2632619.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论