I have a query that returns a SKU with an array of Locations like this:
[{
"Locations": ["TP-401176509", [{
"WarehouseCode": "01",
"LocationCode": "A-04B01-B",
"Quantity": 1,
"Reserve": false
}, {
"WarehouseCode": "01",
"LocationCode": "A-04B02-A",
"Quantity": 1,
"Reserve": false
}, {
"WarehouseCode": "01",
"LocationCode": "A-04B13-A",
"Quantity": 1,
"Reserve": false
}]]
}
Essentially i want to map Locations[0] as a "Sku" object to fill the array in Locations[1]
CodePudding user response:
Use a forEach()
loop to update each element of the array.
const result = [{
"Locations": ["TP-401176509", [{
"WarehouseCode": "01",
"LocationCode": "A-04B01-B",
"Quantity": 1,
"Reserve": false
}, {
"WarehouseCode": "01",
"LocationCode": "A-04B02-A",
"Quantity": 1,
"Reserve": false
}, {
"WarehouseCode": "01",
"LocationCode": "A-04B13-A",
"Quantity": 1,
"Reserve": false
}]]
}];
result.forEach(res => {
let sku = res.Locations[0];
res.Locations[1].forEach(loc => loc.Sku = sku);
});
console.log(result);