I got an array of objects like this:
const defaultActors =
[
{
data: {name: "name1"}
},
{
data: {name: "name2"}
}
]
and a simple array like this:
const actorsToBeAdded = ["nameX", "nameY"]
Is there an easy way to get this:
const combinedActors = [
{
data: {name: "name1"}
},
{
data: {name: "name2"}
},
{
data: {name: "nameX"} //newly added
},
{
data: {name: "nameY"} //newly added
}
]
I'm using useState and trying to get combined array of objects like this:
setCombinedActors(
actorsToBeAdded.map((actor) =>
({
data: {name : actor} //data from [nameX, nameY] array
//also need to spread data from {name: "name1" ...} array
})
)
)
EDIT: thanks for answers. Concat does result in one array but it's array of both "direct" values and objects, like this
["name1", "name2", {Object_with_data},{Object_with_data}]
is there a way to get
[{Object_with_data},{Object_with_data}, {Object_with_data},{Object_with_data}]
or this
["name1", "name2", "name3", "name4"]
? I created sandbox: https://codesandbox.io/s/epic-taussig-zeyev?file=/src/App.js
Thanks to anyone interested!