I have two arrays of objects that I need to merge together to only get one array of object. There is a lot of objects, both have more than 500+ objects
This is an exemple of the structure of the two array of objects :
let API = [
{
actif: true,
id: 8,
creation_user: "David",
date_creation: "févr 4 2022 12:17PM",
description: "A description",
version: "v1r1"
},
{
actif: true,
id: 10,
creation_user: "Julien",
date_creation: "févr 10 2022 12:17PM",
description: "A description",
version: "v1r2"
},
{
actif: false,
id: 20,
creation_user: "Tom",
date_creation: "févr 10 2022 12:17PM",
description: "A description",
version: "v1r2"
}
]
let Parameters = [
{
id: 10,
name: "codeRR",
type: "string"
},
{
id: 20,
name: "codeAA",
type: "string"
},
{
id: 20,
name: "codeCC",
type: "string"
}
]
As we can see, they both have an ID, this ID is the same in both arrays. But not everytime, API can have no parameter.
What i'm trying to achieve is to get in the second array, the "name" and "type", and to add it to the first array (or in a new array).
I've tried with .map on the first array (also tried with the second one) :
let Ressource = api.map(item => Object.assign({}, item, parametre.find(target => target.id === item.id)));
// Result of Ressource
[
{
actif: true,
id: 8,
creation_user: "David",
date_creation: "févr 4 2022 12:17PM",
description: "A description",
version: "v1r1";
name: "codeRR",
type: "string
}
]
// Want I want :
[
{
actif: true,
id: 8,
creation_user: "David",
date_creation: "févr 4 2022 12:17PM",
description: "A description",
version: "v1r1",
parameter: [
{
name: "codeRR",
type: "string
},
{
name: "codePP",
type: "string
}
]
}
]
But when an API have multiple parameter it overwrite the previous one. Is it possible to make something like this in my API
parameter: [{"name": "codeRR", type: "string"}, {"name": "codePP", "type": "string"}]