推送到数组只是在Javascript中读取第二个索引

时间:2014-10-27 12:57:54

标签: javascript arrays push

我有将数组转换为另一个数组的问题。当我尝试推入新数组时,它只是添加第二个索引。

这是我的数组:

var emailAddr = [
    {
        customerID : "C20121121221327_249",
        email : "fanjavaid@gmails.com",
        files : [
            "201409011141106_082895250262",
            "201410011171208_082895250262"
        ]
    },
    {
        customerID : "C20121121221327_300",
        email : "fandi.java@gmail.com",
        files : [
            "201409011141106_xxx",
        ]
    }
];

这里是我创建新数组的代码:

var penampungan = [];
for (var i = 0; i < emailAddr.length; i++) {
    var data = {
        customerID : emailAddr[i].customerID,
        email : emailAddr[i].email
    };

    for (var j = 0; j < emailAddr[i].files.length; j++) {        
        data['master'] = emailAddr[i].files[j] + ".pdf";
        data['detail'] = emailAddr[i].files[j] + "_detail.pdf";

        penampungan.push(data);
    }
}

console.log(penampungan);

我的预期结果是这样的:

var newArray = [
    {
        customerID : "C20121121221327_249",
        email : "fanjavaid@gmail.com",
        master : "201409011141106_082895250262.pdf",
        detail : "201409011141106_082895250262_detail.pdf"
    },
    {
        customerID : "C20121121221327_249",
        email : "fanjavaid@gmail.com",
        master : "201410011171208_082895250262.pdf",
        detail : "201410011171208_082895250262_detail.pdf"
    },
    {
        customerID : "C20121121221327_300",
        email : "fandi.java@gmail.com",
        master : "201409011141106_xxx.pdf",
        detail : "201409011141106_xxx_detail.pdf"
    }
];

如何创建?

谢谢。

3 个答案:

答案 0 :(得分:2)

您需要将data创建移动到内循环

var penampungan = [];
for (var i = 0; i < emailAddr.length; i++) {       
    for (var j = 0; j < emailAddr[i].files.length; j++) {
        var data = {
             customerID : emailAddr[i].customerID,
             email : emailAddr[i].email
        };        
        data['master'] = emailAddr[i].files[j] + ".pdf";
        data['detail'] = emailAddr[i].files[j] + "_detail.pdf";

        penampungan.push(data);
    }
}

答案 1 :(得分:0)

像这样:

var penampungan = [];
for (var i = 0; i < emailAddr.length; i++) {
    for (var j = 0; j < emailAddr[i].files.length; j++) {
        var data = {};
        data.customerID = emailAddr[i].customerID,
        data.email = emailAddr[i].email
        data.master = emailAddr[i].files[j] + ".pdf";
        data.detail= emailAddr[i].files[j] + "_detail.pdf";
        penampungan.push(data);
    }
}
console.log(penampungan);

答案 2 :(得分:0)

首先,为什么不创建一个类?它会为您组织数组的每个元素中的数据,并减少需要解析的代码量。我可以建议像:

function Info (customerID, email, files) {
    this.customerID = customerID;
    this.email = email;
    this.files = files;
    this.penampungan = function () {
        var newObjects = new Array (files.length);
        for (var i = 0; i < files.length; i++) {
            newObjects [i] = {
                customerID : this.customerID,
                email : this.email,
                master : files [i] + ".pdf",
                detail : files [i] + "_detail.pdf",
            };
        }
    return newObjects;
    }
}

现在创建一个for语句,它接受一个I​​nfo数组并将penampungan对象加载到一个新数组中。