what I want to achieve is filling the array "presents" with the value of arr when name is "Mario" in this case, what I get is the array "present" modify, but what I want is an non mutative array, only change the value of the empty array with the value of another array(in this case I put arr as example)
let present= [
{
name: "Peter",
presents: []
},
{
name: "Mario",
presents: []
},
{
name: "Amanda",
presents: []
},
{
name: "David",
presents: []
}
]
arr = ["car","coal"]
const res= present.map(s =>s.name == "Mario" ? s.presents.concat(arr) : [])
console.log(res)
What I want to get:
let present= [
{
name: "Peter",
presents: []
},
{
name: "Mario",
presents: ["car","coal"]
},
{
name: "Amanda",
presents: []
},
{
name: "David",
presents: []
}
]
pd: sorry , I could not explain better, still learning English.