Search code examples
node.jsarraysjsonsortingjavascript-objects

NodeJS Changing all Instances of an Array inside a Nest Object


I have nested array of objects with an array inside of each object.

const nestedArray = [ objectTemp{foo: "", lines: []} ] <---- simplified example of the nested array structure.

I am filling each templated object with different values for foo and the lines array and then pushing it the the 'nestedArray'. When I do this the lines array contains the same values for every object the 'nestedArray'. The foo values are different for each object as they should be. Any ideas why this is happening?

Below is my actually code. orderData is json object contain info about orders This is the main loop that interates through orderData and seperates customers and orders combineData is the 'nestedArray' containing the newOrder Objects. Each newOrder object contains a lines : []. The lines array holds the order line items.

After this loop if you were to print each object in the combinedData array each object has different values as it should. When you print the lines array of each object they contain the same objects. Each line array should contain different objects.

I hope this explanation clearly explains what I'm trying to achieve and what the code is currently doing. Looking forward to some replies. Please let me know if I need to clarify anything.

---------------------------CODE STARTS----------------------


     for(let i = 0; i < orderData.length; i++){
          if(lastOrder === orderData[i]["Order Name"]){
            //Order with multiple line items
            newLine = await fillLineData(orderData[i]);
            await pushToArray(newOrder.lines, newLine);

          }
          else{
            //loop has moved on to next order. Save newOrder Object to array combinedData

            if(lastOrder) {
                await pushToArray(combinedData, newOrder);
                newOrder = await clearObject(newOrder);
               
            }

            //start processing data for next order
            const extKey =  await getCustomerKey(orderData[i]);
            newOrder = await fillCustomerData(orderData[i], extKey);
            newLine = await fillLineData(orderData[i]);
            await pushToArray(newOrder.lines, newLine);
        }

        lastOrder = orderData[i]["Order Name"];
       } 
       await pushToArray(combinedData, newOrder);


       //helper function
     async function pushToArray(arr, obj){
       try{
          arr.push(obj);
       }catch(e){
        console.log("Failed to push order lines to array... Error: ", e);
        }
       }
 async function fillCustomerData(orderData, extKey){
  const newData = Object.create(orderCustomerData);

newData.customer_name = orderData["Customer Name"];
newData.shipto_name = orderData["Customer Name (Shipping)"];
newData.shipto_address_1 = orderData["Shipping Address 1"];
newData.shipto_address_2 = orderData["Shipping Address 2"];
newData.shipto_city = orderData["Shipping City"];
newData.shipto_state = orderData["Shipping Province Code"];
newData.shipto_zip = orderData["Shipping ZIP"];
newData.shipto_country = orderData["Shipping Country"],
newData.billto_name = orderData["Customer Name (Billing)"];
newData.billto_address_1 = orderData["Billing Address 1"];
newData.billto_address_2 = orderData["Billing Address 2"];
newData.billto_city = orderData["Billing City"];
newData.billto_state = orderData["Billing Province Code"];
newData.billto_zip = orderData["Billing ZIP"];
newData.billto_country = orderData["Billing Country"];
newData.po = orderData["Order Name"];
newData.tax_collected = orderData["Total Tax"];
newData.freight_resale = orderData["Shipping Price"];
newData.external_key = extKey;
return newData;
}

Solution

  • Issue has been resolved. I needed to add the lines[] array to fillCustomerData function. The lines array is present in orderCustomerData (my object template containing required fields for an order).

    Since lines wasn't going to be changed until the main loop, I didn't think it needed to be defined in the fillCustomerData. That function now looks like this.

    async function fillCustomerData(orderData, extKey){
    
        const newData = Object.create(orderCustomerData);
    
        newData.customer_name = orderData["Customer Name"];
        newData.shipto_name = orderData["Customer Name (Shipping)"];
        newData.shipto_address_1 = orderData["Shipping Address 1"];
        newData.shipto_address_2 = orderData["Shipping Address 2"];
        newData.shipto_city = orderData["Shipping City"];
        newData.shipto_state = orderData["Shipping Province Code"];
        newData.shipto_zip = orderData["Shipping ZIP"];
        newData.shipto_country = orderData["Shipping Country"],
        newData.billto_name = orderData["Customer Name (Billing)"];
        newData.billto_address_1 = orderData["Billing Address 1"];
        newData.billto_address_2 = orderData["Billing Address 2"];
        newData.billto_city = orderData["Billing City"];
        newData.billto_state = orderData["Billing Province Code"];
        newData.billto_zip = orderData["Billing ZIP"];
        newData.billto_country = orderData["Billing Country"];
        newData.po = orderData["Order Name"];
        newData.tax_collected = orderData["Total Tax"];
        newData.freight_resale = orderData["Shipping Price"];
        newData.external_key = extKey;
        newData.lines = [];
        return newData;
    }