skip to Main Content

I have two 2d arrays as below. Both have same length and their elements also have same length.

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"],
];

While the array1 is ordered, array2 is not. I need to find the indexes in array1 and array2 where the elements have different values like

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

I tried the below code to get the rows that are different in both array1, array2 but couldn’t figure out how to find the column indexes.

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)))

Any help to solve this is much appreciated. Thanks

2

Answers


  1. I made up as per my understanding.

    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);
    

    Try this and if you have any issue on my code, let me know.

    Login or Signup to reply.
  2. Is it right that we need an array to store the position of the wrong index?

    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"],
    ];
    
    /**
     * 
     * @param { string[][] } arr1
     * @param { string[][] } arr2
     * @returns { string[] }
     */
    const findDifferentElements = (arr1, arr2) => {
      return arr1.reduce((acc, cur, idx) => {
        const targetIndex = arr2.findIndex((el) => el[0] === cur[0])
        const target = arr2[targetIndex]
    
        cur.forEach((item, i) => {
          if (item !== target[i]) acc.push(`[{${idx}, ${i}}, {${targetIndex}, ${i}}]`)
        })
    
        return acc
      }, [])
    }
    
    console.log(findDifferentElements(array1, array2))
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search