删除 JavaScript 数组中的特定对象

发布于 2024-12-02 18:09:07 字数 1083 浏览 1 评论 0原文

我正在做一个关于删除数组内的对象的测试...因为这是一个测试,所以这是一个非正式的代码..

<script type="text/javascript">

// initialize array and objects
var fruits = new Array();

var z = {
  test1: "test0",
  test2: "test2"
}

fruits.push(z);
var z2 = {
  test1: "test1",
  test2: "test2"
}
fruits.push(z2);
var z3 = {
  test1: "test2",
  test2: "test2"
}
fruits.push(z3);
var z4 = {
  test1: "test3",
  test2: "test2"
}
fruits.push(z4);
var z5 = {
  test1: "test4",
  test2: "test2"
}
fruits.push(z5);

// display array length
document.write("array length is " + fruits.length + "<br>");

// traverse array
for(var x = 0; x < fruits.length; x++){

  // display object content in array
  document.write(fruits[x].test1 + " ");

  // delete object in array where variable test1 is equal to "test2"
  if(fruits[x].test1 == "test2"){
    fruits.splice(x, 1);
    //document.write("array length is " + fruits.length + "<br>");
  }
}
</script>

现在这个代码工作正常(删除数组上的一个对象),但它删除了我想要的那个之后的一个已删除(在上面的代码中,我想删除索引 2 中的对象,但它删除了索引 3 中的对象)

我在这段代码中做错了什么?

TIA:)

im doing a test on deleting objects inside an array... since this is a test, this is rather an informal code..

<script type="text/javascript">

// initialize array and objects
var fruits = new Array();

var z = {
  test1: "test0",
  test2: "test2"
}

fruits.push(z);
var z2 = {
  test1: "test1",
  test2: "test2"
}
fruits.push(z2);
var z3 = {
  test1: "test2",
  test2: "test2"
}
fruits.push(z3);
var z4 = {
  test1: "test3",
  test2: "test2"
}
fruits.push(z4);
var z5 = {
  test1: "test4",
  test2: "test2"
}
fruits.push(z5);

// display array length
document.write("array length is " + fruits.length + "<br>");

// traverse array
for(var x = 0; x < fruits.length; x++){

  // display object content in array
  document.write(fruits[x].test1 + " ");

  // delete object in array where variable test1 is equal to "test2"
  if(fruits[x].test1 == "test2"){
    fruits.splice(x, 1);
    //document.write("array length is " + fruits.length + "<br>");
  }
}
</script>

now this code works fine (deleting an object on the array) but it deletes the one after the one i want deleted (in the code above, i want to delete the object in index 2, but it deletes the object in index 3)

anything i'm doing wrong in this code?

TIA :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

埋情葬爱 2024-12-09 18:09:07

永远不要在迭代数组时尝试更改数组。相反,将要删除的元素的索引保存在变量中,并在 for 循环后将其删除。

You should never try to alter an array while iterating it. Instead, save the index of the element you want to delete in a variable, and remove it after the for loop.

野心澎湃 2024-12-09 18:09:07

这应该有效:

<script type="text/javascript">

    // initialize array and objects
    var fruits = new Array();

    var z = {
      test1: "test0",
      test2: "test2"
    }

    fruits.push(z);
    var z2 = {
      test1: "test1",
      test2: "test2"
    }
    fruits.push(z2);
    var z3 = {
      test1: "test2",
      test2: "test2"
    }
    fruits.push(z3);
    var z4 = {
      test1: "test3",
      test2: "test2"
    }
    fruits.push(z4);
    var z5 = {
      test1: "test4",
      test2: "test2"
    }
    fruits.push(z5);

    // display array length
    document.write("array length is " + fruits.length + "<br>");

    // traverse array
    for(var x = 0; x < fruits.length; x++){

      // display object content in array
      document.write(fruits[x].test1 + " ");

      // delete object in array where variable test1 is equal to "test2"
      if(fruits[x].test1 == "test2"){
        fruits.splice(x-1, 1);
        //document.write("array length is " + fruits.length + "<br>");
      }
    }
    </script>

This should work:

<script type="text/javascript">

    // initialize array and objects
    var fruits = new Array();

    var z = {
      test1: "test0",
      test2: "test2"
    }

    fruits.push(z);
    var z2 = {
      test1: "test1",
      test2: "test2"
    }
    fruits.push(z2);
    var z3 = {
      test1: "test2",
      test2: "test2"
    }
    fruits.push(z3);
    var z4 = {
      test1: "test3",
      test2: "test2"
    }
    fruits.push(z4);
    var z5 = {
      test1: "test4",
      test2: "test2"
    }
    fruits.push(z5);

    // display array length
    document.write("array length is " + fruits.length + "<br>");

    // traverse array
    for(var x = 0; x < fruits.length; x++){

      // display object content in array
      document.write(fruits[x].test1 + " ");

      // delete object in array where variable test1 is equal to "test2"
      if(fruits[x].test1 == "test2"){
        fruits.splice(x-1, 1);
        //document.write("array length is " + fruits.length + "<br>");
      }
    }
    </script>
谁与争疯 2024-12-09 18:09:07

使用“过滤器”作为 underscore.js 中实现

_.filter(fruits, function (fruit) {
    return fruit.test1 !== "test2";
});

这具有使用快速、本机 JavaScript 方法(“过滤器”)(如果可用)。

Use ''filter'' as implemented in underscore.js:

_.filter(fruits, function (fruit) {
    return fruit.test1 !== "test2";
});

This has the advantage of using a fast, native JavaScript method (''filter'') where available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文