在JavaScript中查找嵌套的重复数组。 (lodash / underscore中的嵌套数组uniq) [英] Finding nested duplicate arrays in JavaScript. (Nested Array uniq in lodash/underscore)

查看:434
本文介绍了在JavaScript中查找嵌套的重复数组。 (lodash / underscore中的嵌套数组uniq)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定JavaScript数组的数组是否包含重复项。这可能吗?我首先试图看看我是否可以删除重复项,然后进行相等检查,但我无法通过第一部分。以下是下划线的回报:

I am trying to determine if an array of JavaScript arrays contains duplicates. Is this possible? I am first trying to see if I can strip the duplicates out and then do an equality check but I cannot get past the first part. Here is what underscore returns:

var arr1 = [[1,2], [2,3], [1,2]];
var arr2 = _.uniq(arr1);

var arraysAreEqual = _.isEqual(arr1, arr2);

console.log(arraysAreEqual, arr1, arr2);
// true

Jsbin: http://jsbin.com/vogumo/1/edit?js,console

任何人都知道确定数组是否包含重复数组的方法吗?

Anyone know of a way to determine if the array contains duplicate arrays?

推荐答案

这有点草率,但是(可能) )

It's a little sloppy, but (possible)

var arr2 = _.uniq(arr1, function(item) {
    return JSON.stringify(item);
});

会给你一个正确的结果

这篇关于在JavaScript中查找嵌套的重复数组。 (lodash / underscore中的嵌套数组uniq)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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