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
I made up as per my understanding.
Try this and if you have any issue on my code, let me know.
Is it right that we need an array to store the position of the wrong index?