简体   繁体   English

尝试从javascript / JQuery中的数组中删除项目

[英]Trying to remove items from an array in javascript/JQuery

I've read several questions about this on StackOverflow, and found some solutions even, but they all seem to do the same weird thing: 我已经在StackOverflow上阅读了有关此问题的几个问题,甚至找到了一些解决方案,但它们似乎都在做同样奇怪的事情:

I have an array (array A) of 8 names: 我有8个名称的数组(数组A):

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

And I have an array (array B) of 1 name: 我有一个名称为1的数组(数组B):

arrayB= ["person1"];

Now, I want to remove all names that do not occur in Array B, from Array A. 现在,我要从阵列A中删除所有不在阵列B中出现的名称。

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. 因此,我写了一个小函数,它遍历数组A中的所有项目,并检查它们是否出现在数组B中。如果没有,我将它们从数组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. 因此,我寻找了一个从数组中删除字符串的函数(在PHP中,这非常容易...),我发现了几种方法,它们都给我带来了完全相同的问题。 In the example below, I chose the cleanest method, using jquery's $.grep: 在下面的示例中,我使用jquery的$ .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, ie "person2", "person4", "person6" and "person8". 如您所见,它仅从arrayA中删除“偶数”项,即“ person2”,“ person4”,“ person6”和“ 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)... 如果我多次执行此函数,则第二次它将仅删除“偶数”项(现在为“ person3”和“ person7”),第三次,它将(最终)删除“ person5”。

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, (ie person3, person5 and person7) are "undefined"... 您可以从控制台日志中看到,第一次运行它时,数组中的所有“奇数”项(即person3,person5和person7)都是“未定义的” ...

You can use do..while loop, Array.prototype.splice() to remove elements from an array 您可以使用do..while循环, Array.prototype.splice()从数组中删除元素

 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); 

This is because when you call this: 这是因为当您调用此命令时:

arrayA= $.grep(arrayA, function(value) {
  return value != removeName;
});

The length of arrayA changes, thus arrayA[k] will move to the position of arrayA[k-1] (when k>i). arrayA的长度发生变化,因此arrayA[k]将移动到arrayA[k-1] (当k> i时)。

So we'd better create a new array to store the filtered items and after the iteration, give its value to arrayA. 因此,我们最好创建一个新数组来存储过滤后的项目,并在迭代后将其值提供给arrayA。

 arrayA= ["person1", "person2", "person3", "person4", "person5", "person6", "person7", "person8"]; arrayB= ["person1","person4"]; var arrayC=[]; for (var i = 0, len = arrayA.length; i < len; i++) { console.log('inArray:'+$.inArray(arrayA[i], arrayB)) if($.inArray(arrayA[i], arrayB) != -1){ arrayC.push(arrayA[i]); } console.log('arrayC now consists of: ' + arrayC); } arrayA=arrayC; 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

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

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