Javascript - 比较并查找二维数组中不常见元素的索引

问题描述 投票:0回答:1

我有两个二维数组,如下所示。两者具有相同的长度,并且它们的元素也具有相同的长度。

const array1 = [
  ["Tom1", "PNG", "Test1", "TRUE", "1,000.00", "abc", "Test1"],
  ["Tom2", "png", "Test3", "TRUE", "3,452.00", "wer", "Test3"],
  ["Tom3", "jpg", "Test1", "TRUE", "3,120.00", "edr", "Test1"],
  ["Tom4", "jpg", "Test2", "TRUE", "4,321.00", "fgt", "Test2"],
  ["Tom5", "jpg", "Test2", "TRUE", "4,321.00", "fgt", "Test2"],
  ["Tom6", "jpg", "Test1", "TRUE", "4,569.00", "tgg", "Test5"],
  ["Tom7", "jpg", "Test1", "TRUE", "4,563.00", "RTE", "Test1"],
];

const array2 = [
  ["Tom2", "png", "Test3", "TRUE", "3,452.00", "wer", "Test3"],
  ["Tom7", "jpg", "Test1", "TRUE", "4,563.00", "RTG", "Test1"],
  ["Tom3", "jpg", "Test1", "TRUE", "3,120.00", "edr", "Test1"],
  ["Tom1", "PNG", "Test1", "TRUE", "1,000.00", "abc", "Test1"],
  ["Tom4", "jpg", "Test2", "TRUE", "4,321.00", "fgt", "Test2"],
  ["Tom5", "jpg", "Test2", "TRUE", "4,321.00", "fgt", "Test2"],
  ["Tom6", "jpg", "Test1", "TRUE", "4,500.00", "tgg", "Test5"],
];

虽然 array1 是有序的,但 array2 不是有序的。我需要找到 array1 和 array2 中的索引,其中元素具有不同的值,例如

output: [{6, 5}, {1, 5}], [{5, 4}, {6, 4}]

我尝试了下面的代码来获取 array1、array2 中不同的行,但不知道如何找到列索引。

const findDifferentElements = (array1, array2) => {
  var indexesInArray1 = []
  var indexesInArray2 = []
  const diffInArray1 = array1.filter(each => !array2.includes(each))
  const diffInArray2 = array2.filter(each => !array1.includes(each))
  console.log(diffInArray1, diffInArray2)

  // find indexes of diff elements
  diffInArray1.forEach(each => indexesInArray1.push(array1.indexOf(each)))
  diffInArray2.forEach(each => indexesInArray2.push(array2.indexOf(each)))

  console.log(diffInArray1.forEach(each => array1.indexOf(each)))
  console.log(diffInArray2.forEach(each => array2.indexOf(each)))
}

findDifferentElements(array1(each => JSON.stringify(each.slice(1))), array2(each => JSON.stringify(each)))

非常感谢任何解决此问题的帮助。谢谢

javascript arrays multidimensional-array
1个回答
0
投票

我按照我的理解编的。

const array1 = [
      ["Tom1", "PNG", "Test1", "TRUE", "1,000.00", "abc", "Test1"],
      ["Tom2", "png", "Test3", "TRUE", "3,452.00", "wer", "Test3"],
      ["Tom3", "jpg", "Test1", "TRUE", "3,120.00", "edr", "Test1"],
      ["Tom4", "jpg", "Test2", "TRUE", "4,321.00", "fgt", "Test2"],
      ["Tom5", "jpg", "Test2", "TRUE", "4,321.00", "fgt", "Test2"],
      ["Tom6", "jpg", "Test1", "TRUE", "4,569.00", "tgg", "Test5"],
      ["Tom7", "jpg", "Test1", "TRUE", "4,563.00", "RTE", "Test1"],
    ];

    const array2 = [
      ["Tom2", "png", "Test3", "TRUE", "3,452.00", "wer", "Test3"],
      ["Tom7", "jpg", "Test1", "TRUE", "4,563.00", "RTG", "Test1"],
      ["Tom3", "jpg", "Test1", "TRUE", "3,120.00", "edr", "Test1"],
      ["Tom1", "PNG", "Test1", "TRUE", "1,000.00", "abc", "Test1"],
      ["Tom4", "jpg", "Test2", "TRUE", "4,321.00", "fgt", "Test2"],
      ["Tom5", "jpg", "Test2", "TRUE", "4,321.00", "fgt", "Test2"],
      ["Tom6", "jpg", "Test1", "TRUE", "4,500.00", "tgg", "Test5"],
    ];

    var output = [];
    var indexesInArray1 = []
var indexesInArray2 = []

    function getIndexOfK(arr, k) {
      for (var i = 0; i < arr.length; i++) {
        var index = arr[i].indexOf(k);
        if (index > -1) {
          return [i, index];
        }
      }
    }
    const findDifferentElements = (array1, array2) => {
      var indexesInArray1 = []
      var indexesInArray2 = []

      for(let i in array1) {
        for(let j in array1[i]){
            if(getIndexOfK(array2, array1[i][j]) == undefined) {
                indexesInArray1.push({i, j})
            }
        }
      }

      for(let i in array2) {
        for(let j in array2[i]){
            if(getIndexOfK(array1, array2[i][j]) == undefined) {
                indexesInArray2.push({i, j})
            }
        }
      }

      output.push(indexesInArray1, indexesInArray2);
      console.log(output)

    }

    findDifferentElements(array1, array2);

尝试一下,如果您对我的代码有任何问题,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.