如何使用Javascript / underscorejs找到包含对象的数组数组的交集? [英] How do I find the intersection of an array of arrays that contain objects using Javascript/underscorejs?

查看:121
本文介绍了如何使用Javascript / underscorejs找到包含对象的数组数组的交集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何找到这组数组的交集:

I can't figure out how to find the intersection of this set of arrays:

[
 [
  {"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]
]

这只是示例数据,真实的设置我发生了很大变化,但具有该结构。我希望返回的交集看起来像这样(交叉对象的单个数组):

This is just sample data, the real set I have changes a lot but with that structure. I want the returned intersection to look like this (a single array of the intersected objects):

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

我和LoDashjs以及Underscorejs一起试过这个:

I tried this together with LoDashjs and 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创建一个标签系统。我有一个分类标签按钮的布局,在点击时写入过滤器可观察数组,唯一剩下的就是找到此可观察数组中包含的过滤产品的交集。

I need this because I am trying to create a tags system using knockoutjs. I have a layout of categorized tag buttons that write to a "filter" observable array on click, the only thing left is to find the intersection of the filtered products that are contained in this observable array.

请帮助我,我一直试图连续两天解决这个问题,但缺乏javascript知识来解决这个问题。提前致谢!

Please help me out, I've been trying to solve this for two days straight but lack the javascript knowledge to figure it out. Thanks in advance!

推荐答案

尝试添加申请方法:

  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"}

这篇关于如何使用Javascript / underscorejs找到包含对象的数组数组的交集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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