在数组数组上使用唯一的lodash [英] using lodash unique on an array of arrays

查看:114
本文介绍了在数组数组上使用唯一的lodash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定如何使用它从主阵列中删除重复的阵列.因此请考虑以下几点:

I am not sure how to use this to remove duplicate arrays from the main array. so consider the following:

var arr = [
    [0, 1],
    [0, 1],
    [2, 1],
];

结果数组应如下所示:

var arr = [
    [0, 1],
    [2, 1],
];

该文档对单个元素数组有意义,但对于2d数组则没有意义.

The documentation makes sense for a single array of elements but not a 2d array.

想法?

要变为唯一的数组可能具有:

The array to be turned unique might have:

var arr = [
  [0, 1]
  [0, 2]
  [0, 3]
  [0, 1]
  [2, 1]
]

该数组旁边的数组应该具有所有值进行比较,因此在上面的示例中,它应该吐出:

the array in side the array should have all value compared, so in the above example, it should spit out:

var arr = [
  [0, 1]
  [0, 2]
  [0, 3]
  [2, 1]
]

重复项为[0, 1]

推荐答案

最简单的方法可能是使用uniq方法,然后将内部数组转换为某些字符串表示形式,JSON.stringify应该是足够好的解决方案.

The easiest way would probably be to use uniq method and then convert the inner arrays to some string representation, JSON.stringify should be good enough solution.

var arr = [[0, 1], [0, 2], [0, 3], [0, 1], [2, 1]]
_.uniq(arr, function(item) { 
    return JSON.stringify(item); 
});
// [[0,1], [0,2], [0,3], [2,1]]

这篇关于在数组数组上使用唯一的lodash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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