用循环复杂对象的数组过滤 [英] Filtering arrays of complex objects with loops

查看:217
本文介绍了用循环复杂对象的数组过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我内使用循环寻找符合条件的值的复杂nexted对象的两个数组和if语句如下所示。当我找到一个符合条件的对象,我需要周围循环的下一次去时过滤的对象了。我试图做到这一点与数组,你可以在这里看到,但它不工作作为数组循环的每次迭代中开始。下面的版本是我的code的简化版本。

我想使这些值未在嵌套循环重复基于if语句中数组2以更新值。相反,我emptyArray保持为空,而不是从数组2添加值作为数组2的元素都等于数组的元素。

需要明确的是,现在仍然是emptyArray空,从来没有过滤数组2。我想看到emptyArray在外环的第二个迭代开始收藏价值2那么我想看到emptyArray在外环的第四次迭代开始收藏价值4。

我愿意,因为他们成为emptyArray的一部分,这样他们不会在外环的第6和第8次迭代掀起if语句每个值从数组2过滤。我想,emptyArray = [2,4]和数组2 = [6,8,10]当环路结束。

底线,我需要emptyArray收集符合条件的值,并将其传递回VAR数组2用于过滤的循环过程。请记住,这是阵列的简化版本,并强调基础的解决方案将是非常复杂,我来实现,或为你成功地建议没有更多的细节。

我的code:

 变种数组= [1,2,3,4,1,2,3,4〕;
变种数组2 = [2,4,6,8,10];
变种emptyArray = [];
对于(i = 0; I< array.length,我++){
     VAR东西=阵列[我]
     VAR数组2 = _.without(数组2,emptyArray);
     用于:(a = 0;一个与所述; array2.length;一个++){
         VAR值=数组2 [A];
          如果(东西===值){
              emptyArray.push(值);
              打破;
          }
     }
}


解决方案

有几件事情错了你的code,但为什么你认为推不工作的原因是因为你在里面覆盖的数组2循环。

推不会被调用,因为你的for循环时看到你正在做一个空数组2 VAR数组2 = _.without(数组2,emptyArray);

基本上 VAR数组2 = _.without(数组2 / *这是空的,你只要推翻了它在此范围内* / emptyArray); 总是会导致一个空数组和你的循环将退出,因为长度是从一开始就array2.length === 0。

此外,要使用 _。区别而不是 _。没有

\r
\r

VAR阵列= [1,2,3,4,1,2,3 ,4〕;\r
变种数组2 = [2,4,6,8,10];\r
变种emptyArray = [];\r
\r
对于(VAR I = 0; I< array.length,我++){\r
  VAR东西=阵列[我]\r
  数组2 = _.difference(数组2,emptyArray);\r
  对于(VAR J = 0; J< array2.length; J ++){\r
    VAR值=数组2 [J]。\r
    如果(东西===值){\r
      emptyArray.push(值);\r
      打破;\r
    }\r
  }\r
}\r
\r
的console.log(数组2,数组2);\r
的console.log(emptyArray,emptyArray);

\r

&LT;脚本src=\"https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.5.0/lodash.js\"></script>\r
\r
\r

 数组2 [6,8,10]
emptyArray [2,4]

I have two arrays of complex nexted objects that I'm looking for qualifying values within using loops and if statements as seen below. When I find a qualifying object, I need to filter that object out during the next go around of the loop. I'm trying to do that with an array as you can see here but it isn't working as the array starts over during each iteration of the loop. The following version is a simplified version of my code.

I want to update the values in array2 based on the if statement so that those values are not repeated in the nested loop. Instead my emptyArray remains empty instead of adding values from the array2 as elements of array2 are equal to elements of array.

To be clear, right now emptyArray remains empty and never filters array2. I'd like to see emptyArray collect value 2 at the start of the outer loop's second iteration then I'd like to see emptyArray collect value 4 at the start of the 4th iteration of the outer loop.

I'd want to filter each of these values from array2 as they become part of emptyArray so that they do not set off the if statement during the 6th and 8th iterations of the outer loop. I imagine that emptyArray = [2, 4] and array2 = [6, 8, 10] when the loops are finished.

Bottom line, I need emptyArray to collect the qualifying values and pass them back to var array2 for filtering as the loop processes. Remember this is a simplified version of the arrays, and underscore based solution would be very complicated for me to implement or for you to successfully suggest without much more detail.

My code:

var array = [1, 2, 3, 4, 1, 2, 3, 4];
var array2 = [2, 4, 6, 8, 10];
var emptyArray = [];
for (i = 0; i < array.length; i++){
     var something = array[i];
     var array2 = _.without(array2, emptyArray);
     for (a = 0; a < array2.length; a++){
         var value = array2[a];
          if(something === value){
              emptyArray.push(value);
              break;
          }
     }
}

解决方案

There are a few things wrong with your code, but the reason why you think that push isn't working is because you are overriding your array2 inside the loop.

The push never gets called because your for loop sees an empty array2 when you are doing var array2 = _.without(array2, emptyArray);

Basically var array2 = _.without(array2 /* this is empty, you just overrode it in this scope */, emptyArray); will always result in an empty array and your for loop will exit because length is array2.length === 0 from the start.

Also, you want to use _.difference instead of _.without

var array = [1, 2, 3, 4, 1, 2, 3, 4];
var array2 = [2, 4, 6, 8, 10];
var emptyArray = [];

for (var i = 0; i < array.length; i++) {
  var something = array[i];
  array2 = _.difference(array2, emptyArray);
  for (var j = 0; j < array2.length; j++) {
    var value = array2[j];
    if (something === value) {
      emptyArray.push(value);
      break;
    }
  }
}

console.log("array2",array2);
console.log("emptyArray", emptyArray);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.5.0/lodash.js"></script>

array2 [6, 8, 10]
emptyArray [2, 4]

这篇关于用循环复杂对象的数组过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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