针对另一个对象数组过滤对象数组

时间:2017-01-13 14:55:27

标签: javascript arrays underscore.js lodash

我有两个像这样简化的数组。

var a = [{
 number: 1,
 name: "A"
}, {
 number: 2,
 name: "B"
}, {
 number: 3,
 name: "C"
}, {
 number: 4,
 name: "D"
}, {
 number: 5,
 name: "E"
}, {
 number: 6,
 name: "F"
}];

var b = [{
 number: 3,
 name: "C"
}, {
 number: 6,
 name: "F"
}];

我想要的是返回一个数组,其中a被过滤为b。结果就是这样。

var result = [{
 number: 1,
 name: "A"
}, {
 number: 2,
 name: "B"
}, {
 number: 4,
 name: "D"
}, {
 number: 5,
 name: "E"
}];

在堆栈溢出处查看了很多其他解决方案,但无法使其工作。

我可以使用像下划线这样的库。

6 个答案:

答案 0 :(得分:6)

通过单行解决方案可以实现lodash



var a = [{
 number: 1,
 name: "A"
}, {
 number: 2,
 name: "B"
}, {
 number: 3,
 name: "C"
}, {
 number: 4,
 name: "D"
}, {
 number: 5,
 name: "E"
}, {
 number: 6,
 name: "F"
}];

var b = [{
 number: 3,
 name: "C"
}, {
 number: 6,
 name: "F"
}];

var result = _.differenceWith(a, b, _.isEqual);

console.log(result)

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

您可以使用Array#filter。这样的事情:

const filterData = (a,b) => {
    return a.filter( (itemA) => {
        return b.some( (itemB) => { 
            return (itemA.number === itemB.number); 
         });
  });
}

我刚刚创建了一个小提琴来测试代码:https://jsfiddle.net/mrlew/pb1qqeyd/3/(你必须打开控制台来检查结果)。

或......单行:

const filterData = (a,b) => a.filter( (itemA) => b.some( (itemB) => (itemA.number === itemB.number) ) );

编辑:感谢@kzh建议,编辑后使用.some

答案 2 :(得分:2)

var a = [{
 number: 1,
 name: "A"
}, {
 number: 2,
 name: "B"
}, {
 number: 3,
 name: "C"
}, {
 number: 4,
 name: "D"
}, {
 number: 5,
 name: "E"
}, {
 number: 6,
 name: "F"
}];

var b = [{
 number: 3,
 name: "C"
}, {
 number: 6,
 name: "F"
}];

var _ = require('lodash');
var result = _.differenceBy(a,b,'name');

有关更多信息,请参阅Lodash文档:https://lodash.com/docs/#differenceBy

答案 3 :(得分:1)

您可以使用JavaScript中的内置filter函数来过滤具有另一个数组的数组,请检查以下代码段。

var a = [{
  number: 1,
  name: "A"
}, {
  number: 2,
  name: "B"
}, {
  number: 3,
  name: "C"
}, {
  number: 4,
  name: "D"
}, {
  number: 5,
  name: "E"
}, {
  number: 6,
  name: "F"
}];

var b = [{
  number: 3,
  name: "C"
}, {
  number: 6,
  name: "F"
}];

var result = a.filter(function(currentValue, index, arr) {
  var found = false;
  for (var i = 0; i < b.length; i++) {
    if (currentValue.number === b[i].number) {
      found = true;
      break;
    }
  }

  if (!found) {
    return currentValue;
  }
});

console.log(result);

答案 4 :(得分:0)

您可以使用filter()some()every()使用普通javascript执行此操作。

var a = [{"number":1,"name":"A"},{"number":2,"name":"B"},{"number":3,"name":"C"},{"number":4,"name":"D"},{"number":5,"name":"E"},{"number":6,"name":"F"}]
var b = [{"number":3,"name":"C"},{"number":6,"name":"F"}]

var result = a.filter(function(o) {
  return !b.some(function(e) {
    return Object.keys(o).length == Object.keys(e).length && Object.keys(o).every(function(k) {
      return e[k] == o[k]
    })
  })
})

console.log(result)

答案 5 :(得分:0)

var v1 = JSON.parse(a);
var v2 = JSON.parse(b);

var v3 = [] ;


function objectEquals(v1, v2) {

    if (typeof(v1) !== typeof(v2)) {
        return false;
    }
if (v1 instanceof Object && v2 instanceof Object) {

  for (k in v1) {
            r = objectEquals(v1[k], v2[k]);
            if (!r) {
              v3.push(v1[k]);  

            }
        }



}

}

// call the above method passing your two object, and return a new unique array


objectEquals(v1, v2) ;