I have an array of objects that I would like to simplify to make it easy to search/filter when put in a table. I'm trying to take this multi-dimensional array and push it into a single dimension array. I'm able to pull the first level out, but I keep getting undefined for the keys in below details. The code I have is clearly not working, but it is returning close to the amount of values I need at least. Any ideas?
var transferArray = [{
"transferCode": "1041",
"details": [{
"vendor": ["AL", "PA", "TX"],
"voucherNumber": ["123", "456", "789"],
"description": ["test", "test1", "test2"],
"amount": [543, 768, 123]
}]
},
{
"transferCode": "23421",
"details": [{
"vendor": ["CA", "AK", "SD", "WA"],
"voucherNumber": ["1213", "4896", "769", "765"],
"description": ["test", "test1", "test2", "test3"],
"amount": [53,468, 903, 2134]
}]
}]
//the structure I'd like
[{
"transferCode": "1041",
"vendor": "AL",
"voucherNumber": "123",
"description": "test",
"amount": 543
},
{
"transferCode": "1041",
"vendor": "PA",
"voucherNumber": "456",
"description": "test1",
"amount": 768
}]
//JS I tried but can't get working. It's just returning undefined for vendor, voucherNumber, description, and amount
var newTransferArray = [];
transferArray.forEach(function(sTransferCode) {
transferArray.forEach(function(oDetails) {
newTransferArray.push({
transferCode: sTransferCode.transferCode,
vendor: oDetails.vendor,
voucherNumber: oDetails.voucherNumber,
description: oDetails.description,
amount: oDetails.amount
})
})
})
console.log(newTransferArray)