使用forEach时,splice不会被删除?

时间:2018-05-09 08:44:27

标签: javascript node.js

当产品Line.Id与退款ID匹配时,它应该从退款变量中删除该对象。

退款变量应该只剩下1行,即:

refunds = {
  Lines: [
  {LineId: "444"}
  ]
}

我做错了什么?

参见示例:https://jsfiddle.net/ua8f1a5x/8/



 products = {
      Items: [{
          Name: "Item Name 1",
          LineId: "111",
          Status: [],
        },
        {
          Name: "Item Name 2",
          LineId: "222",
          Status: [],
        },
        {
          Name: "Item Name 3",
          LineId: "333",
          Status: [],
        }
      ]
    }
    
    refunds = {
      Lines: [
      {LineId: "222"},
      {LineId: "111"},
      {LineId: "444"}
      ]
    }
    
    
     refunds.Lines.forEach((refundItem, refundIndex) => {
        console.log("Checking Id " + refundItem.LineId);
         
         products.Items.forEach((Item) => {
             if (refundItem.LineId == Item.LineId) {
                 Item.Status.push({Name: "Refunded"});
                 
                 //Delete  object from refund
                 refunds.Lines.splice(refundIndex, 1);
             }
         });
     });
     
     
     console.log(refunds);
     console.log(products);




3 个答案:

答案 0 :(得分:2)

forEach迭代静态数组,就像调用forEach时一样(并且正如所说的那样,在你重复迭代它时会改变一个对象导致混乱)。在这种情况下,您应该使用filter代替:



const products = {
  Items: [{
      Name: "Item Name 1",
      LineId: "111",
      Status: [],
    },
    {
      Name: "Item Name 2",
      LineId: "222",
      Status: [],
    },
    {
      Name: "Item Name 3",
      LineId: "333",
      Status: [],
    }
  ]
}

const refunds = {
  Lines: [{
      LineId: "222"
    },
    {
      LineId: "111"
    },
    {
      LineId: "444"
    }
  ]
}

refunds.Lines = refunds.Lines.filter(refundItem => {
  console.log("Checking Id " + refundItem.LineId);
  const foundProduct = products.Items.find(({
    LineId
  }) => LineId === refundItem.LineId);
  if (foundProduct) {
    foundProduct.Status.push({
      Name: "Refunded"
    });
    return false;
  }
  return true;
});


console.log(refunds);
console.log(products);




答案 1 :(得分:1)

您通常只使用filter()

products = {
  Items: [{
      Name: "Item Name 1",
      LineId: "111",
      Status: [],
    },
    {
      Name: "Item Name 2",
      LineId: "222",
      Status: [],
    },
    {
      Name: "Item Name 3",
      LineId: "333",
      Status: [],
    }
  ]
}

refunds = {
  Lines: [{
      LineId: "222"
    },
    {
      LineId: "111"
    },
    {
      LineId: "444"
    }
  ]
}

refunds.Lines = refunds.Lines.filter(e => ! products.Items.some(x => x.LineId === e.LineId));
console.log(refunds)

答案 2 :(得分:1)

  

我做错了什么?

当正在迭代的数组也发生变异时,

refundIndex没有被占用。

改为使用filter

refunds.Lines.filter( s => !products.Items.find( t => t.LineId == s.LineId ) );

<强>演示

var products = {
  Items: [{
      Name: "Item Name 1",
      LineId: "111",
      Status: [],
    },
    {
      Name: "Item Name 2",
      LineId: "222",
      Status: [],
    },
    {
      Name: "Item Name 3",
      LineId: "333",
      Status: [],
    }
  ]
};

var refunds = {
  Lines: [
  {LineId: "222"},
  {LineId: "111"},
  {LineId: "444"}
  ]
};

refunds.Lines = refunds.Lines 
                .filter( s => !products.Items.
                    find( t => t.LineId == s.LineId ) );

console.log( refunds );