NodeJS 更改嵌套对象内数组的所有实例

问题描述 投票:0回答:1

我嵌套了对象数组,每个对象内部都有一个数组。

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

我用 foo 和lines 数组的不同值填充每个模板化对象,然后将其推送到“nestedArray”。当我这样做时,行数组对于“nestedArray”的每个对象都包含相同的值。每个对象的 foo 值应该是不同的。有什么想法为什么会发生这种情况吗?

下面是我的实际代码。 orderData 是 json 对象,包含有关订单的信息 这是通过 orderData 进行交互并分离客户和订单的主循环 mergeData 是包含 newOrder 对象的“nestedArray”。每个 newOrder 对象都包含一行:[]。 lines 数组保存订单行项目。

在此循环之后,如果要打印组合数据数组中的每个对象,每个对象都有不同的值。 当您打印每个对象的行数组时,它们包含相同的对象。每个线数组应包含不同的对象。

我希望这个解释清楚地解释了我想要实现的目标以及代码当前正在做什么。期待一些回复。如果我需要澄清任何事情,请告诉我。

----------------------------------------代码开始-------------------- --


     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;
}
node.js arrays json sorting javascript-objects
1个回答
0
投票

问题已解决。我需要将lines[] 添加到fillCustomerData 函数中。 lines 数组存在于 orderCustomerData(我的对象模板包含订单所需字段)中。由于行数组在主循环之前不会更改,因此我认为不需要在 fillCustomerData 函数中定义它。该函数现在看起来像这样。

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;

}

© www.soinside.com 2019 - 2024. All rights reserved.