尝试从javascript / JQuery中的数组中删除项目 [英] Trying to remove items from an array in javascript/JQuery

查看:50
本文介绍了尝试从javascript / JQuery中的数组中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在StackOverflow上阅读了有关此问题的几个问题,甚至找到了一些解决方案,但它们似乎都在做同样奇怪的事情:



我有一个数组(数组A)的8个名称:

  arrayA = [ person1, person2, person3, person4 , person5, person6, person7, person8]; 

我有一个名称为1的数组(数组B):

  arrayB = [ person1]; 

现在,我想删除所有出现在数组中的名称B,来自数组A。



所以我写了一个小函数,它遍历数组A中的所有项目,并检查它们是否出现在数组B中。如果没有,我从数组A中删除了它们。



所以我寻找了一个从数组中删除字符串的函数(在PHP中,这非常容易...),并且我发现了几种方法,它们都给我带来完全相同的问题。在下面的示例中,我使用jquery的$ .grep选择了最干净的方法:



  arrayA = [ person1, person2, person3, person4, person5, person6, person7, person8]; arrayB = [ person1]; = 0,len = arrayA.length; i  

 < script src = https:// ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js\"></script>  

>



如您所见,它仅从arrayA中删除偶数项,即 person2, person4, person6和 person8。 / p>

如果我多次执行此函数,则第二次它将仅删除偶数项(现在为 person3和 person7),第三项时间,它最终会删除 person5 ...



有人可以告诉我我没看到什么吗?
您可以从控制台日志中看到,第一次运行它时,数组中的所有奇数项目(即person3,person5和person7)都是未定义的 ...

解决方案

您可以使用 do..while 循环和 Array。 prototype.splice()从数组中删除元素



  arrayA = [ vincent, Rumpelstilzchen, LuckeR, Nordland, Siegfried, NeKrone, Carnage, tom59fr]; arrayB = [ vincent]; var i = 0;执行{if(arrayB [0]!== arrayA [i]){arrayA.splice(i,1); } else {++ i; }} while(i< arrayA.length);删除i; console.log(arrayA,arrayB);  


I've read several questions about this on StackOverflow, and found some solutions even, but they all seem to do the same weird thing:

I have an array (array A) of 8 names:

arrayA= ["person1", "person2", "person3", "person4", "person5", "person6", "person7", "person8"];

And I have an array (array B) of 1 name:

arrayB= ["person1"];

Now, I want to remove all names that do not occur in Array B, from Array A.

So I wrote a little function which loops through all items in Array A, and checks if they occur in Array B. If they do not, I remove them from Array A.

So I looked for a function to remove strings from an array (in PHP, this is all so much easier...), and I found several methods, which all give me exactly the same problem. In the example below, I chose the cleanest method, using jquery's $.grep:

arrayA= ["person1", "person2", "person3", "person4", "person5", "person6", "person7", "person8"];
arrayB= ["person1"];

for (var i = 0, len = arrayA.length; i < len; i++) {
			
  if($.inArray(arrayA[i], arrayB) == -1){

    var removeName= arrayA[i];

    console.log('Removing row of: ' + removeName);

    /*
    $('tr[player=\'' + removeName + '\']').find('td')
    .wrapInner('<div style="display: block;" />')
    .parent()
    .find('td > div')
    .slideUp(700, function(){
      $(this).parent().parent().remove();
    });
    */

    arrayA= $.grep(arrayA, function(value) {
      return value != removeName;
    });
    
    console.log('arrayA now consists of: ' + arrayA);

  }

}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

As you can see, it only removes the "even" items from arrayA, i.e. "person2", "person4", "person6" and "person8".

If I execute this function multiple times, the 2nd time it removes again only the "even" items (which is now "person3" and "person7"), and the third time, it removes "person5" (finally)...

Can someone please tell me what I'm not seeing? You can see from the console log that, the first time you run it, all the "odd" items in the array, (i.e. person3, person5 and person7) are "undefined"...

解决方案

You can use do..while loop, Array.prototype.splice() to remove elements from an array

arrayA= ["vincent"
        , "Rumpelstilzchen"
        , "LuckeR"
        , "Nordland"
        , "Siegfried"
        , "NeKrone"
        , "Carnage"
        , "tom59fr"];
arrayB= ["vincent"];

var i = 0;

do {
  if (arrayB[0] !== arrayA[i]) {
    arrayA.splice(i, 1);  
  } else {
  ++i;
  }
} while (i < arrayA.length);
delete i;

console.log(arrayA, arrayB);

这篇关于尝试从javascript / JQuery中的数组中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆