拆分数组而不在数组内创建数组的问题

时间:2017-05-23 13:52:15

标签: javascript arrays

我有一个propertyArray,每个属性都有一个或多个费用 我想从propertyArray中取出费用并将它们全部放入expenseArray

我的问题是它在一个数组中生成一个数组。

我该如何避免?

    for (var i=0; i<propertyArray.length; i++) {
        expenseArray[i] = propertyArray[i].expense;
        delete propertyArray[i].expense;
    }

这是propertyArray

propertyArray: [
    {
        "_id": "ANA-C202",
        "name": "Ananya",
        "expense": [
            {
                "_id": 2,
                "expenseCategory": "OFFICE",
                "dueDate": 1493719324,
                "amount": 8121,
                "account": "BANK01",
            }
        ]
    },
    {
        "_id": "PAP-720",
        "name": "Paradise Park",
        "expense": [
            {
                "_id": 1,
                "expenseCategory": "OFFICE",
                "dueDate": 1493719324,
                "amount": 100000,
                "account": "BANK01",
            },
            {
                "_id": 3,
                "expenseCategory": "CONDOBILL",
                "dueDate": 1493719324,
                "amount": 3123,
                "account": "BANK01",
            },
            {
                "_id": 5,
                "expenseCategory": "CONDOBILL",
                "dueDate": 1493719324,
                "amount": 925,
                "account": "BANK02",
            }
        ]
    }
}

propertyArray在运行后看起来很好,但expenseArray却没有......

expenseArray: [
    [
        {
            "_id": 2,
            "expenseCategory": "OFFICE",
            "dueDate": 1493719324,
            "amount": 8121,
            "account": "BANK01",
        }
    ],
    [
        {
            "_id": 1,
            "expenseCategory": "OFFICE",
            "dueDate": 1493719324,
            "amount": 100000,
            "account": "BANK01",
        },
        {
            "_id": 3,
            "expenseCategory": "CONDOBILL",
            "dueDate": 1493719324,
            "amount": 3123,
            "account": "BANK01",
        },
        {
            "_id": 5,
            "expenseCategory": "CONDOBILL",
            "dueDate": 1493719324,
            "amount": 925,
            "account": "BANK02",
        }
    ]

2 个答案:

答案 0 :(得分:2)

尝试使用concat

&#13;
&#13;
var propertyArray = [{
    "_id": "ANA-C202",
    "name": "Ananya",
    "expense": [{
      "_id": 2,
      "expenseCategory": "OFFICE",
      "dueDate": 1493719324,
      "amount": 8121,
      "account": "BANK01",
    }]
  },
  {
    "_id": "PAP-720",
    "name": "Paradise Park",
    "expense": [{
        "_id": 1,
        "expenseCategory": "OFFICE",
        "dueDate": 1493719324,
        "amount": 100000,
        "account": "BANK01",
      },
      {
        "_id": 3,
        "expenseCategory": "CONDOBILL",
        "dueDate": 1493719324,
        "amount": 3123,
        "account": "BANK01",
      },
      {
        "_id": 5,
        "expenseCategory": "CONDOBILL",
        "dueDate": 1493719324,
        "amount": 925,
        "account": "BANK02",
      }
    ]
  }
];

var expensesArray = [];

// Use concat to get the desired result
propertyArray.forEach(function(property) {
  expensesArray = expensesArray.concat(property.expense);
  delete property.expense;
});

console.log(expensesArray);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

迭代数组中的每个对象并推送它而不是推送整个数组。请参阅下面的代码或实例https://jsfiddle.net/b17moyvv/1/

var propertyArray = [
{
    "_id": "ANA-C202",
    "name": "Ananya",
    "expense": [{
        "_id": 2,
        "expenseCategory": "OFFICE",
        "dueDate": 1493719324,
        "amount": 8121,
        "account": "BANK01"
    }]
}, {
    "_id": "PAP-720",
    "name": "Paradise Park",
    "expense": [{
            "_id": 1,
            "expenseCategory": "OFFICE",
            "dueDate": 1493719324,
            "amount": 100000,
            "account": "BANK01"
        },
        {
            "_id": 3,
            "expenseCategory": "CONDOBILL",
            "dueDate": 1493719324,
            "amount": 3123,
            "account": "BANK01"
        },
        {
            "_id": 5,
            "expenseCategory": "CONDOBILL",
            "dueDate": 1493719324,
            "amount": 925,
            "account": "BANK02"
        }
    ]
}
];
console.log(propertyArray);

var expenseArray = [];
for (var i = 0; i < propertyArray.length; i++) {
    // iterate each object inside the array and push it instead of the whole array
    for (var j = 0; j < propertyArray[i].expense.length; j++){
        expenseArray.push(propertyArray[i].expense[j]);
    }
    delete propertyArray[i].expense;
}
console.log(expenseArray);