【问题标题】:splice is not deleting when using forEach?使用 forEach 时拼接不删除?
【发布时间】:2018-05-09 08:44:26
【问题描述】:

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

refund 变量应该只剩下 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);

【问题讨论】:

标签: javascript node.js


【解决方案1】:

forEach 迭代 static 数组,就像调用 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);

【讨论】:

    【解决方案2】:

    你通常只使用 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)

    【讨论】:

    • 我的推送状态如何?
    【解决方案3】:

    我做错了什么?

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-03
      • 1970-01-01
      • 1970-01-01
      • 2021-11-16
      • 2013-06-26
      • 2012-12-24
      • 1970-01-01
      相关资源
      最近更新 更多