【问题标题】:How to reduce a 2D array in TypeScript to return a 2D array of unique subarrays?如何减少 TypeScript 中的二维数组以返回唯一子数组的二维数组?
【发布时间】:2021-05-20 01:50:28
【问题描述】:

我正在尝试减少我的二维数组,以便没有重复的子数组。我使用了 .reduce() 方法来解决这个问题,但是我的条件语句似乎不像人们想象的那样工作。

请参阅标准输出以获得更多清晰度。如果您知道如何进行这项工作或可能的解决方法,请告诉我。

const unique = result.reduce<number[][]>((accum, curr) => {
        if (!accum.includes(curr)) {
            console.log('accum:', accum);
            console.log('curr', curr);
            return [...accum, curr];
        }
        else{
            return accum;   
        }
    }, []);

console.log('\nresult', result);
console.log('unique', unique);

/*
stdout:
accum: []
curr [ 0, -1, 1 ]
accum: [ [ 0, -1, 1 ] ]
curr [ 0, -1, 1 ]
accum: [ [ 0, -1, 1 ], [ 0, -1, 1 ] ]
curr [ 2, -1, -1 ]

result [ [ 0, -1, 1 ], [ 0, -1, 1 ], [ 2, -1, -1 ] ]
unique [ [ 0, -1, 1 ], [ 0, -1, 1 ], [ 2, -1, -1 ] ]


desired output:
unique [ [ 0, -1, 1 ], [ 2, -1, -1 ] ]
*/

【问题讨论】:

    标签: node.js typescript multidimensional-array types reduce


    【解决方案1】:

    只需使用Set。请记住,它仅适用于原始值数组

    
    const makeUnique = <T extends string | number | symbol>(arr: T[]) => [...new Set(arr)];
    
    
    const unique2D = [[1, 1, 2], [2, 2, 2, 1], [3, 3, 3]].reduce<number[][]>((acc, elem) =>
      [...acc, makeUnique(elem)]
      , []
    )
    

    Playground

    更新

    
    const makeUnique = <T extends string | number | symbol>(arr: T[]) => [...new Set(arr)];
    
    type MainType = number[]
    const unique2D = (arr: Array<MainType>) =>
      arr.reduce<Array<MainType>>((acc, elem) => [...acc, makeUnique(elem)], [])
    
    const getDublicates = (arr1: Array<MainType>, arr2: MainType) => {
      return arr1.reduce((acc, elem, index) =>
        elem.length === arr2.length && arr2.every(el => elem.includes(el))
          ? [...acc, index]
          : acc
        , [] as MainType)
    };
    
    
    const check = (arr: Array<MainType>) => {
      const dublicates = arr.reduce((acc, elem) => makeUnique([...acc, ...getDublicates(arr, [...elem]).slice(1)]), []);
      return arr.filter((_, index) => !dublicates.includes(index))
    };
    
    const result = check(unique2D([[0, -1, 1], [2, -1, -1], [3, 3], [3], [3], [4], [4]]));
    

    【讨论】:

    • 我尝试了makeUnique(result) 并得到了...“'number[][]' 类型的参数不能分配给'(string | number | symbol)[]' 类型的参数。”
    • 请记住,如果您有对象数组,Set 将不起作用
    • 非常感谢您的帮助,但我正在尝试删除二维数组中的重复数组。您的解决方案会删除数组中的重复值。
    • @Shah 我做了一个更新,请尝试,它会从二维数组中删除复制。检查unique2D
    • 您的解决方案将 [[1, 1, 2], [2, 2, 2, 1], [3, 3, 3]] 更改为 [ [ 1, 2 ], [ 2, 1 ], [3] ] 太棒了!但我正在寻找这样的东西: [ [ 0, -1, 1 ], [ 0, -1, 1 ], [ 2, -1, -1 ] ] 到 [ [ 0, -1, 1 ], [ 2, -1, -1 ] ]
    【解决方案2】:

    我找到了一个暂时可行的解决方案。如果有人可以用 Set 或 .reduce() 解决它,请告诉我。

        const result: number[][] = new Array(); //results obtained later on in code
        
        const Unique: {[key: string]: number} = {}; //store unique results in a hash
        const solution: number[][] = new Array();
        let target;
        
        //remove possible duplicates:
        for (let i = 0; i < result.length; i++) {
            let key = JSON.stringify(result[i]);
            if (!Unique[key]) {
                Unique[key] = 0;
            }
        }
        for (let key in Unique) {
            solution.push(JSON.parse(key))
        }
        
        return solution;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-22
      • 2011-01-31
      • 2021-03-16
      相关资源
      最近更新 更多