I am working with a Datasource that contains a nested array of objects. I successfully selected key value pairs, and now my goal is to add those values to the top level of the object, outside of the nested object.
Here is the original array:
data= [
{
"flowId": 7079,
"flowName": "jackson-demo",
"version": 1,
"CreatedDate": "2020-04-02",
"UpdateDate": "",
"LastRunDate": "2020-04-02",
"active": false,
"properties": [
{
"id": 7080,
"key": "country",
"value": "in",
"category": "General"
},
{
"id": 7081,
"key": "source",
"value": "hive",
"category": "General"
}
]
},
{
"flowId": 7079,
"flowName": "jackson-demo",
"version": 1,
"CreatedDate": "2020-04-02",
"UpdateDate": "",
"LastRunDate": "2020-04-02",
"active": false,
"properties": [
{
"id": 7080,
"key": "country",
"value": "au",
"category": "General"
},
{
"id": 7081,
"key": "source",
"value": "aws",
"category": "General"
}
]
} ]
The code snippet below allows me to extract the key value pairs:
for (var i = 0; i < data.length; i++) {
data[i].properties.forEach((arrayItem, i) => {
if (arrayItem.key === 'country') {
console.log('Key: ' + arrayItem.key + ' ' + 'Value: ' + arrayItem.value);
}
});
}
Output generated by the code:
Key: country Value: au
Key: country Value: in
My question now is, how can I insert these extracted values back into the array to achieve the following structure:
data= [
{
"flowId": 7079,
"flowName": "jackson-demo",
"version": 1,
"CreatedDate": "2020-04-02",
"UpdateDate": "",
"LastRunDate": "2020-04-02",
"active": false,
"country": "in"
"properties": [
{
"id": 7080,
"key": "country",
"value": "in",
"category": "General"
},
{
"id": 7081,
"key": "source",
"value": "hive",
"category": "General"
}
]
},
{
"flowId": 7079,
"flowName": "jackson-demo",
"version": 1,
"CreatedDate": "2020-04-02",
"UpdateDate": "",
"LastRunDate": "2020-04-02",
"active": false,
"country":"au"
"properties": [
{
"id": 7080,
"key": "country",
"value": "au",
"category": "General"
},
{
"id": 7081,
"key": "source",
"value": "aws",
"category": "General"
}
]
} ]