【问题标题】:How to remove a nested JS object inside a loop? [duplicate]如何删除循环内的嵌套 JS 对象? [复制]
【发布时间】:2017-12-17 23:06:33
【问题描述】:

我正在尝试在属性与我的参数匹配时删除特定对象(此处当轮子的名称为“Michelin”时)但我无法使其工作...

我该怎么做?

var car = {
  type: "Fiat",
  model: "500",
  color: "White",
  wheels: [{
    name: "Goodyear",
    color: "Red"
  }, {
    name: "Goodyear",
    color: "Yellow"
  }, {
    name: "Goodyear",
    color: "Black"
  }, {
    name: "Michelin",
    color: "Blue"
  }]
};

$.each(car.wheels, function() {
  if (this.name == "Michelin") {
    delete this;
  }
})
console.log(car);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

【问题讨论】:

  • 你不能在函数中使用“this”。使用迭代对象。
  • 回调函数有 2 个参数。第一个是索引,第二个是迭代对象,用那个。
  • Alexandru 的回答很到位。另外,我建议从不使用$.each()。这是一个混乱的功能,是零碎发展的。在 Alexandru 的代码中使用 .forEach() 或更合适的函数,如 .filter()。如果您出于某种原因必须使用$.each()(不,真的,不要!)至少不要在回调中使用this。这让事情变得非常混乱。相反,请使用回调的两个命名参数,如提到的 doublesharp。

标签: javascript jquery arrays function


【解决方案1】:

$.each 方法的 回调 函数有两个参数:

$.each(car.wheels, function(i,item) {
   if (item.name == "Michelin") {
       delete car.wheels[i];
   }
});

但这不是最佳解决方案。通常,delete 运算符用于删除对象的属性。如果您使用它在数组上,delete 将删除数组项,但不会重新索引 数组或更新其长度。这使它看起来好像是未定义的:

var car = {
      type: "Fiat",
      model: "500",
      color: "White",
      wheels: [{
        name: "Goodyear",
        color: "Red"
      }, {
        name: "Goodyear",
        color: "Yellow"
      }, {
        name: "Goodyear",
        color: "Black"
      }, {
        name: "Michelin",
        color: "Blue"
      }]
};
$.each(car.wheels, function(i,item) {
    if (item.name == "Michelin") {
       delete car.wheels[i];
    }
});
console.log(car);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

解决方案是使用filter 方法,该方法接受一个回调提供的函数,应用于数组中的每个项目。

var car = {
      type: "Fiat",
      model: "500",
      color: "White",
      wheels: [{
        name: "Goodyear",
        color: "Red"
      }, {
        name: "Goodyear",
        color: "Yellow"
      }, {
        name: "Goodyear",
        color: "Black"
      }, {
        name: "Michelin",
        color: "Blue"
      }]
 };
car.wheels=car.wheels.filter(function(wheel){
  return wheel.name!='Michelin';
});
console.log(car);

【讨论】:

  • @doublesharp,现在看看。
  • 在添加$.each() 代码的编辑之前,您的答案更好。 :-) .filter() 是一个很好的解决方案。 $.each() 是一个混乱的函数,最好避免使用这样的代码。
  • 完美运行,谢谢!另外,@MichaelGeary,谢谢你的评论,从现在开始我会尽量使用你提到的功能!
  • @MichaelGeary,我知道..delete 运算符通常用于删除对象的属性。我想向 OP 展示如何使用每种方法。
【解决方案2】:

使用 jQuery grep 函数:

var car = {
  type: "Fiat",
  model: "500",
  color: "White",
  wheels: [{
    name: "Goodyear",
    color: "Red"
  }, {
    name: "Goodyear",
    color: "Yellow"
  }, {
    name: "Goodyear",
    color: "Black"
  }, {
    name: "Michelin",
    color: "Blue"
  }]
};
car.wheels= $.grep(car.wheels,function(wheel){
  return wheel.name!='Michelin';
});
console.log(car);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

【讨论】:

    猜你喜欢
    • 2018-02-07
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-23
    相关资源
    最近更新 更多