I am working with two arrays of objects:
firstAry = [{
"status": "Creating",
"datacenter-id": "1test",
"datacenter-name": "1name"
}, {
"status": "Creating",
"datacenter-id": "2test",
"datacenter-name": "2name"
}, {
"status": "Creating",
"datacenter-id": "id1"&,
"datacenter-name": "data6"
}, {
"status": "Creating",
"datacenter-id": "id2",
"datacenter-name": "data7"
}]
secondAry = [
{
"status": "Creating",
"cluster-id": "1test",
"cluster-name": "clu1",
"datacenter-id": "null"
},
{
"status": "Creating",
"cluster-id": "1test1",
"cluster-name": "clu1",
"datacenter-id": "id1"
},
{
"status": "Creating",
"cluster-id": "1test113",
"cluster-name": "clu11",
"datacenter-id": "id1"
},
{
"status": "Creating",
"cluster-id": "2test2",
"cluster-name": "clu2",
"datacenter-id": "id2"
},
{
"status": "Creating",
"cluster-id": "2test22",
"cluster-name": "clu22",
"datacenter-id": "id2"
}
]
I am looking to create a table with the following fields: status
, cluster-name
, and datacenter-name
. However, the datacenter-name
is split between the two arrays, and I need to merge them based on the datacenter-id
. If there is no matching datacenter-id
, then the datacenter-name
field should be blank. I want to extract the datacenter-name
from the firstAry
array and add it to the corresponding object in the secondAry
array.
The resulting array of objects should be structured like this:
Result = [
{ "status": "Creating", "cluster-id": "1test", "cluster-name": "clu1", "datacenter-id": "null" ,"datacenter-name": "null" },
{ "status": "Creating", "cluster-id": "1test1", "cluster-name": "clu1", "datacenter-id": "id1", "datacenter-name": "data6" },
{ "status": "Creating", "cluster-id": "1test113", "cluster-name": "clu11", "datacenter-id": "id1" , "datacenter-name": "data6" },
{ "status": "Creating", "cluster-id": "2test2", "cluster-name": "clu2", "datacenter-id": "id2" ,"datacenter-name": "data7" },
{ "status": "Creating", "cluster-id": "2test22", "cluster-name": "clu22", "datacenter-id": "id2" ,"datacenter-name": "data7" } ]
I have attempted to use the filter method, but it is removing elements with no matching id. Can you please assist in achieving this?