6

我不知道如何找到这组数组的交集:

[
 [
  {"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知识来解决这个问题。提前致谢!

4

4 回答 4

6

尝试添加它们的应用方法:

  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"}
于 2013-02-06T07:48:39.383 回答
3

这是我使用的一种方法,似乎效果很好。

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>

于 2016-09-07T00:46:49.713 回答
1

如果您只比较对象本身,您可能会遇到错误,因为这将返回 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;
};
于 2013-02-06T08:13:16.790 回答
1

根据https://lodash.com/docs#intersectionBy

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

于 2016-06-21T15:47:56.360 回答