I have a data structure like this:
let foo = [{ "a": [1,2,3] }]
I want to map over the array inside the object, inside this array
// Approach 1:
foo.map(foo => foo.a.map(val => val + 1))
This returns [ [ 2,3,4 ] ] but removes the outer object (which I consider quite interesting).
// Approach 2:
foo.map(foo => {
foo.a = foo.a.map(val => val + 1)
return foo
})
This works, but also changes the object itself.
Is there some way to do this, that returns the correct value, but doesn't change the object given as an argument?
EDIT:
My use case includes having multiple objects in this array:
let bar = [
{ "a": [1,2,3] },
{ "a": [5,5,5] },
{ "a": [10,100,1000] },
]
So just addressing the array inside directly doesn't really help.
foo[0].a.map(val => val + 1)