使用JavaScript将多维对象数组转换为一维数组

时间:2016-01-14 07:35:36

标签: javascript arrays

我有一个对象数组,我想简化这些对象,以便在放入表格时轻松搜索/过滤。我试图采用这个多维数组并将其推入单维数组中。我能够拉出第一级,但我仍然未定义details下面的键。我的代码显然不起作用,但它至少接近我需要的值。有什么想法吗?

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)

3 个答案:

答案 0 :(得分:1)

我希望这会有所帮助。

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);

答案 1 :(得分:1)

只收集内部数组的指定索引的每个数据。

&#13;
&#13;
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>');
&#13;
&#13;
&#13;

答案 2 :(得分:-1)

应该做的伎俩

 transferArray.map(function(it) {

  var mrgd = Object.assign({},it, it.details[0]);
  delete mrgd.details;
  res.push(mrgd);

});