如何过滤2d数组? [重复]

时间:2021-07-04 21:36:25

This question already has an answer here:

这个问题在这里已有答案:

What is best way to filter 2d arr and return also 2d but with only unique arrays as elements: [[7,3,7],[7,7,3],[3,7,7],[3,7,7],[7,7,3],[7,3,7]] should return [[7,3,7],[7,7,3],[3,7,7]] ?

什么是过滤2d arr并返回2d但只有唯一数组作为元素的最佳方法:[[7,3,7],[7,7,3],[3,7,7],[3,7, 7],[7,7,3],[7,3,7]应该返回[[7,3,7],[7,7,3],[3,7,7]]?

1 个解决方案

#1


0  

Try this:

Working Example JSBin

工作示例JSBin

var uniques = function(items) {
    var map = {};
    return items.filter(function(item) {
        return item in map ? false : map[item] = true;
    });
};

#1


0  

Try this:

Working Example JSBin

工作示例JSBin

var uniques = function(items) {
    var map = {};
    return items.filter(function(item) {
        return item in map ? false : map[item] = true;
    });
};