简体   繁体   English

从数组中查找并删除元素

[英]Find and remove element from array

If I have a javascript array of numbers如果我有一个 javascript 数字数组

[1, 2, 5, 7, 5, 4, 7, 9, 2, 4, 1]

And I want to search through that array and remove a particular number like 4 giving me我想搜索该数组并删除一个特定的数字,例如 4 给我

[1, 2, 5, 7, 5, 7, 9, 2, 1]

What's the best way to do that最好的方法是什么

I was thinking it might look like我在想它可能看起来像

for(var i = 0; i < myarray.length; i++) {
    if(myarray[i] == 4) {
        myarray.remove(i)
    }
}

But there is no remove function for an array.但是数组没有remove函数。 Also if I remove an element from the array it messes up my i unless I correct it.此外,如果我从数组中删除一个元素,它会弄乱我的i除非我纠正它。

You can use .splice() to remove one or more items from an array and if you iterate from back to front of the array, your indexing doesn't get messed up when you remove an item.您可以使用.splice()从数组中删除一个或多个项目,如果从数组的后到前进行迭代,则在删除项目时索引不会被弄乱。

var arr = [1, 2, 5, 7, 5, 4, 7, 9, 2, 4, 1];
for (var i = arr.length - 1; i >= 0; i--) {
    if (arr[i] == 4) {
        arr.splice(i, 1);
    }
}

personally, i like to use a re-usable function with the filter method:就个人而言,我喜欢在 filter 方法中使用可重用的函数:

//generic filter:
function without(a){return this!=a;}


//your data:
var r= [1, 2, 5, 7, 5, 4, 7, 9, 2, 4, 1];

//your data filtered against 4:
var no4=r.filter(without, 4);

//verify no 4s:
alert(no4); //shows: "1,2,5,7,5,7,9,2,1"

if you want this to mutate the original array, you can just wipe and push the new values into old array:如果您希望它改变原始数组,您可以擦除并将新值推送到旧数组中:

 function without(a){return this!=a;}
 var r= [1, 2, 5, 7, 5, 4, 7, 9, 2, 4, 1],  //orig
    r2=r.slice(); //copy
 r.length=0; //wipe orig
 [].push.apply( r, r2.filter(without, 4)); //populate orig with filtered copy
 r; // == [1, 2, 5, 7, 5, 7, 9, 2, 1]

John Resig , creator of jQuery created a very handy Array.remove method that I always use it in my projects. jQuery 的创建者John Resig创建了一个非常方便的 Array.remove 方法,我总是在我的项目中使用它。

// Array Remove - By John Resig (MIT Licensed)
    Array.prototype.remove = function(from, to) {
      var rest = this.slice((to || from) + 1 || this.length);
      this.length = from < 0 ? this.length + from : from;
      return this.push.apply(this, rest);
    };

So, you can use your code like this:所以,你可以像这样使用你的代码:

// Remove the second item from the array
myarray.remove(1);
// Remove the second-to-last item from the array
myarray.remove(-2);
// Remove the second and third items from the array
myarray.remove(1,2);
// Remove the last and second-to-last items from the array
myarray.remove(-2,-1);

---Edit---- - -编辑 - -

for(var i = 0; i < myarray.length; i++) {
    if(myarray[i] == 4) {
        myarray.remove(i);
    }
}

Use your code like this to remove specific value.像这样使用您的代码来删除特定值。

Here is a remove function based on index这是一个基于索引的删除函数

function  remove(array, index){
     for (var i = index; i < arr.length-1; i++) {
          array[i] = array[i+1];    
      }
}

Basically, what this does is shifts all the elements from the index to the "left."基本上,这样做是将所有元素从索引移到“左”。 Not quite sure how splice works, but i'm guessing it works exactly the same way.不太确定 splice 是如何工作的,但我猜它的工作方式完全相同。

After adding that function to your code all you have to do is.将该函数添加到您的代码后,您所要做的就是。

for(var i = 0; i < myarray.length; i++) {
    if(myarray[i] == 4) {
       remove(myarray,i);
    }
}

I prefer do something like this:我更喜欢做这样的事情:

removeEmail(event){
myarray.splice(myarray.indexOf(event.target.id), 1)
}

myaraay.splice() Going to remove myarray.indexOf() gives the number or whatever you wan to remove from inside the array. myaraay.splice() 要删除 myarray.indexOf() 会给出数字或您想要从数组内部删除的任何内容。 Thats the simplest way without need a loop over.这是最简单的方法,不需要循环。 :) :)

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

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