【问题标题】:JavaScript get elements from an object array that are not in anotherJavaScript 从对象数组中获取不在另一个对象数组中的元素
【发布时间】:2018-11-23 00:17:46
【问题描述】:

我是 JavaScript 编程新手,我有两个具有以下结构的对象数组:

myFirstObjArray = [{foo: 1, bar: 1}, {foo: 3, bar: 3}, {foo: 4, bar: 5}];
mySecondObjArray = [{foo: 2}, {foo: 4}, {foo: 5}];

我需要获取两个单独的数组,其中包含键 foo 的值,第一个数组包含第一个数组中的数组,但第二个数组中的数组基于键 foo 的值,第二个数组包含在mySecondObjArray 中,但不在myFirstObjArray 中。

有没有办法做到这一点

for(i=0;i<myFirstObjArray.length;i++)
   for(j=0;j<mySecondObjArray .length;j++)
      {...build first array here}

for(i=0;i<mySecondObjArray .length;i++)
   for(j=0;j<myFirstObjArray.length;j++)
      {...build second array here}

?可能我的问题是我没找到的重复问题,所以请温柔。

预期输出:

firstArray = [{foo: 1}, {foo: 3}];
secondArray = [{foo: 2}, {foo: 5}];

【问题讨论】:

标签: javascript arrays javascript-objects


【解决方案1】:

您可以通过设置基于其他数组元素的条件来简单地过滤一个数组的元素。

var myFirstObjArray = [{foo: 1, bar: 1}, {foo: 3, bar: 3}, {foo: 4, bar: 5}],
    mySecondObjArray = [{foo: 2}, {foo: 4}, {foo: 5}],
    
    firstArray  = myFirstObjArray.filter(o=> !mySecondObjArray.some(i=> i.foo === o.foo));
    
    secondArray  = mySecondObjArray.filter(o=> !myFirstObjArray.some(i=> i.foo === o.foo));
    
    console.log(firstArray.map(o=> {return {'foo' :  o.foo}}))
    console.log(secondArray.map(o=> {return {'foo' :  o.foo}}))

附:

some() 方法测试数组中的至少一个元素是否通过了提供的函数实现的测试。我添加了一个函数,它只检查 foo 属性是否存在于另一个具有相同值的数组中,以便能够从第一个数组中过滤。

最后你可以使用.map来过滤掉想要的键值对

希望这是有道理的

阅读更多关于.somefilter的信息

【讨论】:

  • 第一个数组应该只包含 foo 属性,所以 OP 可能也需要阅读 map
  • 不提供预期的输出
【解决方案2】:

这是一个小解决方案,只有filter 和一个带有foo 属性的map

const myFirstObjArray = [{foo: 1, bar: 1}, {foo: 3, bar: 3}, {foo: 4, bar: 5}];
const mySecondObjArray = [{foo: 2}, {foo: 4}, {foo: 5}];

const exclude = (arr1, arr2) => arr1.filter(o1 => arr2.map(o2 => o2.foo).indexOf(o1.foo) === -1);

console.log(exclude(myFirstObjArray, mySecondObjArray));
console.log(exclude(mySecondObjArray, myFirstObjArray));

【讨论】:

  • 首选includes 优于indefOf
【解决方案3】:

ES5 不使用粗箭头,

var myFirstObjArray = [{foo: 1, bar: 1}, {foo: 3, bar: 3}, {foo: 4, bar: 5}],
    mySecondObjArray = [{foo: 2}, {foo: 4}, {foo: 5}],
    
    firstArray  = myFirstObjArray.filter(function(o) { return !mySecondObjArray.some(function(i) { return i.foo === o.foo})});
     
    secondArray  = mySecondObjArray.filter(function(o) { return !myFirstObjArray.some(function(i) { return i.foo === o.foo})});
    
    console.log(firstArray)
    console.log(secondArray)

【讨论】:

    【解决方案4】:

    您可以通过查找进行过滤。

    const unique = a => o => !a.some(({ foo }) => o.foo === foo);
    
    var first = [{foo: 1, bar: 1}, {foo: 3, bar: 3}, {foo: 4, bar: 5}],
        second = [{foo: 2}, {foo: 4}, {foo: 5}],
        uniqueFirst = first.filter(unique(second)),
        uniqueSecond = second.filter(unique(first));
        
    console.log(uniqueFirst);
    console.log(uniqueSecond);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

      【解决方案5】:

      您可以创建一个可重用的函数来防止代码重复。只需切换到函数参数。另请注意,内部循环是简单的for 循环,因此我们可以使用break 并避免不必要的检查。

      var firstArray = [];
      var secondArray = [];
      var myFirstObjArray = [{foo: 1, bar: 1}, {foo: 3, bar: 3}, {foo: 4, bar: 5}];
      var mySecondObjArray = [{foo: 2}, {foo: 4}, {foo: 5}];
      
      function difference(myFirstObjArray, mySecondObjArray){
        var firstArray = [];
        myFirstObjArray.forEach((obj)=>{
         var match = false;
         for(var i=0; i<mySecondObjArray.length; i++){
           var secondObj = mySecondObjArray[i];
           if(obj.foo === secondObj.foo){
             match = true;
             break;
           }
         }
         if(!match){
           firstArray.push({'foo': obj.foo});
         }
        });
        return firstArray;
      }
      
      
      console.log(difference(myFirstObjArray, mySecondObjArray));
      
      console.log(difference(mySecondObjArray, myFirstObjArray));

      【讨论】:

        猜你喜欢
        • 2016-11-19
        • 1970-01-01
        • 1970-01-01
        • 2019-07-04
        • 1970-01-01
        • 2016-05-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多