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)
I hope this helps and what u need.
var newArray = [];
for(i = 0; i < transferArray.length; i++){
var obj = transferArray[i];
for(x = 0; x < obj.details[0].vendor.length; x++){
var tempObj = {};
tempObj["transferCode"] = obj.transferCode;
tempObj["vendor"] = obj.details[0].vendor[x];
tempObj["voucherNumber"] = obj.details[0].voucherNumber[x];
tempObj["description"] = obj.details[0].description[x];
tempObj["amount"] = obj.details[0].amount[x];
newArray.push(tempObj);
}
}
console.log(newArray);
Just collect every data for the specified index of the inner arrays.
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] }] }]; function newArray(data) { var array = []; data.forEach(function (item) { item.details.forEach(function (detail) { var keys = Object.keys(detail), length = keys.reduce(function (r, a) { return Math.max(r, detail[a].length); }, 0), object, i; for (i = 0; i < length; i++) { object = { transferCode: item.transferCode }; keys.forEach(function (k) { object[k] = detail[k][i]; }); array.push(object); } }); }); return array; } document.write('<pre>' + JSON.stringify(newArray(transferArray), 0, 4) + '</pre>');
should do the trick
transferArray.map(function(it) {
var mrgd = Object.assign({},it, it.details[0]);
delete mrgd.details;
res.push(mrgd);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.