简体   繁体   English

删除javascript数组中的特定对象

[英]deleting a specific object in a javascript array

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) 现在此代码可以正常工作(在数组上删除一个对象),但是在我要删除的对象之后删除一个(在上面的代码中,我想删除索引2中的对象,但是它删除了索引3中的对象)

anything i'm doing wrong in this code? 我在这段代码中做错了什么?

TIA :) TIA :)

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. 而是将要删除的元素的索引保存在变量中,然后在for循环之后将其删除。

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>

Use ''filter'' as implemented in underscore.js: 使用underscore.js中实现的“过滤器”:

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

This has the advantage of using a fast, native JavaScript method (''filter'') where available. 这具有在可用时使用快速的本机JavaScript方法(“过滤器”)的优势。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM