如何找到包含使用Javascript / underscorejs对象数组的数组的交集?(How d

2019-07-19 08:26发布

我无法弄清楚如何找到这个阵列集的交集:

[
 [
  {"name":"product1","light":"1"},
  {"name":"product2","light":"2"},
  {"name":"product5","light":"5"},
  {"name":"product4","light":"4"}
 ],
 [
  {"name":"product2","light":"2"},
  {"name":"product3","light":"3"},
  {"name":"product4","light":"4"}
 ],[...more arrays with objects]
]

这仅仅是样本数据 ,真正集我改变了很多,但与结构。 我想返回的路口,看起来像这样(相交对象的单个阵列):

[
 {"name":"product2","light":"2"},
 {"name":"product4","light":"4"},
]

我LoDashjs和Underscorejs一起尝试这样的:

_.intersectionObjects = _.intersect = function(array) {
var slice = Array.prototype.slice; // added this line as a utility
var rest = slice.call(arguments, 1);
return _.filter(_.uniq(array), function(item) {
  return _.every(rest, function(other) {
    //return _.indexOf(other, item) >= 0;
    return _.any(other, function(element) { return _.isEqual(element, item); });
  });
});
};

我需要这个,因为我试图创建一个使用knockoutjs一个标签系统。 我有写上点击一个“过滤器”可观察到的阵列分类标签按钮的布局,唯一剩下的就是找到那些包含在此观察到阵列的过滤产品的交叉点。

请帮我,我一直在试图直接解决这一两天,但缺乏的JavaScript知识弄明白。 提前致谢!

Answer 1:

尝试将它们应用的方法:

  var myArr = [
    [
      {"name":"product1","light":"1"},
      {"name":"product2","light":"2"},
      {"name":"product5","light":"5"},
      {"name":"product4","light":"4"}
    ],
    [
      {"name":"product2","light":"2"},
      {"name":"product3","light":"3"},
      {"name":"product4","light":"4"}
    ]
  ]

  _.intersectionObjects = _.intersect = function(array) {
    var slice = Array.prototype.slice;
    var rest = slice.call(arguments, 1);
    return _.filter(_.uniq(array), function(item) {
      return _.every(rest, function(other) {
        return _.any(other, function(element) {
          return _.isEqual(element, item);
        });
      });
    });
  };

  var myIntersection = _.intersectionObjects.apply(_, myArr);

  for (var i = 0; i < myIntersection.length; i++) {
    console.log(myIntersection[i]);
  }

  // Sample Output:
  // Object {name: "product2", light: "2"}
  // Object {name: "product4", light: "4"}


Answer 2:

下面是我使用的方法似乎运作良好。

 var arr1 = [{"id":"1"},{"id":"2"},{"id":"3"}]; var arr2 = [{"id":"1"},{"id":"3"}]; function match(item){ var isMatch = _.matcher(item); var matches = _.filter(arr2, isMatch); return matches[0]; } var matchArray = _.compact(_.map(arr1, function(val){ return match(val)})); document.write(JSON.stringify(matchArray)); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> 



Answer 3:

你很可能如果你只是比较对象本身横跨错误运行,因为这将返回false:

var o1 = {"foo":"bar"};
var o2 = {"foo":"bar"};
return o1 == o2;

你需要的值进行比较的对象的内部,以及基于这些相交:

在这里的jsfiddle做你喜欢什么。 http://jsfiddle.net/turiyag/bWrQW/6/

function valueIntersect(a1, a2) {
    var aReturn = [];
    for(i1 in a1) {
        o1 = a1[i1];
        for (i2 in a2) {
            o2 = a2[i2];
            if(equals(o1,o2)) {
                aReturn.push(o1);
                break;
            }
        }
    }
    return aReturn;
}

function equals(o1, o2) {
    if (!o2 || o1.length != o2.length) {
        return false;
    }
    for (i in o1) {
        if (o1[i] !== o2[i]) {
            return false;
        }
    }
    return true;
};


Answer 4:

按https://lodash.com/docs#intersectionBy

_.intersectionBy([arrays], [iteratee=_.identity])



文章来源: How do I find the intersection of an array of arrays that contain objects using Javascript/underscorejs?