0

i have problem to convert array to another array. When i try to push in new array, it just add second index.

Here is my Array :

var emailAddr = [
    {
        customerID : "C20121121221327_249",
        email : "[email protected]",
        files : [
            "201409011141106_082895250262",
            "201410011171208_082895250262"
        ]
    },
    {
        customerID : "C20121121221327_300",
        email : "[email protected]",
        files : [
            "201409011141106_xxx",
        ]
    }
];

Here my code to create new array :

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

My expected result is like this :

var newArray = [
    {
        customerID : "C20121121221327_249",
        email : "[email protected]",
        master : "201409011141106_082895250262.pdf",
        detail : "201409011141106_082895250262_detail.pdf"
    },
    {
        customerID : "C20121121221327_249",
        email : "[email protected]",
        master : "201410011171208_082895250262.pdf",
        detail : "201410011171208_082895250262_detail.pdf"
    },
    {
        customerID : "C20121121221327_300",
        email : "[email protected]",
        master : "201409011141106_xxx.pdf",
        detail : "201409011141106_xxx_detail.pdf"
    }
];

How to create that?

Thank you.

1
  • why do you need to unfold your files field? Commented Oct 27, 2014 at 13:06

3 Answers 3

2

You need to move data creation to the inner loop

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);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

very minor, but you could of course make it all consistent and set master and detail in the data = {} too
0

Like this:

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

Comments

0

First of all, why not create a class? It would organize the data in each element of your array for you and reduce the amount of code needed to be parsed. May I suggest something like:

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

Now make a for statement that takes an Info array and loads the penampungan objects into a new array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.