简体   繁体   English

比较两个对象的JavaScript数组并删除常见的数组对象

[英]Comparing two JavaScript Arrays of Objects and removing common Array Objects

var a = [
  [
    {
      id: "AAA"
    },
    {
      id: "BBB"
    }
  ],
  [
    {
      id: "AAA"
    },
    {
      id: "DDD"
    }
  ]
];
var b = [
  [
    {
      id: "BBB"
    }
  ],
  [
    {
      id: "CCC"
    },
    {
      id: "BBB"
    }
  ],
  [
    {
      id: "AAA"
    }
  ],
  [
    {
      id: "AAA"
    },
    {
      id: "DDD"
    }
  ],
  [
    {
      id: "DDD"
    }
  ],
  [
    {
      id: "CCC"
    },
    {
      id: "DDD"
    }
  ],
  [
    {
      id: "AAA"
    }
  ],
  [
    {
      id: "AAA"
    },
    {
      id: "BBB"
    }
  ]
];

function remove_duplicates(a, b) {
    for (var i = 0, len = a.length; i < len; i++) {
        for (var j = 0, len = b.length; j < len; j++) {
            if (a[i].name == b[j].name) {
                b.splice(j, 1);
            }
        }
    }

    console.log(a);
    console.log(b);

}

console.log(a);
console.log(b);

remove_duplicates(a,b);

I have tried the filter and reduce but they are coming with desired result I found a similar one but the one I have got little different structure 我尝试过过滤器并减小过滤器,但是它们达到了预期的效果,我发现了类似的过滤器,但结构却几乎没有什么不同

seeking for possible solution from JavaScript or underscore 从JavaScript或下划线寻求可能的解决方案

expected result: [[{"id":"BBB"}],[{"id":"CCC"},{"id":"BBB"}],[{"id":"AAA"}],[‌​{"id":"DDD"}],[{"id"‌​:"CCC"},{"id":"DDD"}‌​]] 预期结果: [[{"id":"BBB"}],[{"id":"CCC"},{"id":"BBB"}],[{"id":"AAA"}],[‌​{"id":"DDD"}],[{"id"‌​:"CCC"},{"id":"DDD"}‌​]]

First we need to know when two arrays are equal. 首先,我们需要知道两个数组何时相等。 I have defined this in the isEqual function such that an array is equal to another array if they are the same length and the objects have the same id in the same order. 我在isEqual函数中定义了它,以便如果数组的长度相同并且对象以相同的顺序具有相同的ID,则该数组等于另一个数组。

Next, we need to understand what it means to 'remove duplicates'. 接下来,我们需要了解“删除重复项”的含义。 Imagine if you have two arrays of numbers. 假设您有两个数字数组。 [1,2,3,4] and [2,4,5,6]. [1,2,3,4]和[2,4,5,6]。 We'll expect the result to be [1,3,5,6]. 我们期望结果为[1,3,5,6]。 We do this by getting the first array and subtracting any items that exist in the second array and then getting the second array and subtracting any items that exist in the first array. 为此,我们获取第一个数组并减去第二个数组中存在的所有项,然后获取第二个数组并减去第一个数组中存在的任何项。 Finally we join the two results together. 最后,我们将两个结果结合在一起。 This is what is happening in the remove_duplicates function. 这就是remove_duplicates函数中发生的事情。

var a = [
  [
    {
      id: "AAA"
    },
    {
      id: "BBB"
    }
  ],
  [
    {
      id: "AAA"
    },
    {
      id: "DDD"
    }
  ]
];
var b = [
  [
    {
      id: "BBB"
    }
  ],
  [
    {
      id: "CCC"
    },
    {
      id: "BBB"
    }
  ],
  [
    {
      id: "AAA"
    }
  ],
  [
    {
      id: "AAA"
    },
    {
      id: "DDD"
    }
  ],
  [
    {
      id: "DDD"
    }
  ],
  [
    {
      id: "CCC"
    },
    {
      id: "DDD"
    }
  ],
  [
    {
      id: "AAA"
    }
  ],
  [
    {
      id: "AAA"
    },
    {
      id: "BBB"
    }
  ]
];

function remove_duplicates(a, b) {
  var setOfB = setOf(b);
  var setOfA = setOf(a);

  var aMinusB = minus(setOfA, setOfB);
  var bMinusA = minus(setOfB, setOfA);
  return aMinusB.concat(bMinusA);
}

function setOf(array){
  return _.uniq(array, false, JSON.stringify);
}

function minus(a,b) {
  return _.reject(b, function(item){
    return _.find(a, _.partial(_.isEqual, item));
  });
}


// var expected = [
//   [{"id":"BBB"}],
//   [{"id":"CCC"},{"id":"BBB"}],
//   [{"id":"AAA"}],
//   [{"id":"DDD"}],
//   [{"id":"CCC"},{"id":"DDD"}]
// ];


console.log(remove_duplicates(a,b));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM