Search code examples
javascriptnode.jsnodes

print difference value available in array using node js


I don't know how this is possible i want to print array which contain same value and difference value.

I want to check if the value is present in both the array if present then print it in one array which contain all the same value of both array and another array which contain name which is difference.

readFileArray:
[
  [
    mainFolder/abc_doc.txt, 
    mainFolder/pqr_doc.txt, 
    mainFolder/subFolder/xyz_image.jpg,
    mainFolder/subFolder/iop_pdf.pdf,
  ],
  [
    Fish,
    Life,
    Qwerty,
    Moon
  ]
]
comparePathName:
[
  mainFolder/abc_doc.txt, 
  mainFolder/pqr_doc.txt,
  mainFolder/subFolder/xyz_image.jpg, 
  mainFolder/subFolder/iop_pdf.pdf, 
  mainFolder/fish.txt, 
  mainFolder/life.txt,
  mainFolder/subFolder/qwerty.jpg, 
  mainFolder/subFolder/moon.pdf, 
]

code:

for (let i = 0; i <= readFileArray.length - 1; i++) {
      for (let j = 0; j < readFileArray[i].length - 1; j++) {
        if (readFileArray[i][j] === comparePathName) {
          availableName = readFileArray[1][j];
           if (typeof availableName !== undefined) {
             console.log(availableName)
           }
        }
      }
    }

output: the value which are present in both the array i am getting it in availableName

availableName= 
[
  mainFolder/abc_doc.txt, 
  mainFolder/pqr_doc.txt,
  mainFolder/subFolder/xyz_image.jpg,
  mainFolder/subFolder/iop_pdf.pdf,
]

Now I also need the non-similar value in my new array which name is expectedArray

expectedArray=
[
  mainFolder/fish.txt,  
  mainFolder/life.txt, 
  mainFolder/subFolder/qwerty.jpg, 
  mainFolder/subFolder/moon.pdf, 
]

Solution

  • One simple way to do it is using array.filter and includes methods:

    To get the common values, filter will iterate the first array and includes will check the second array for the current value. To get the difference, check if the current value is not found in the second array.

    let availableName = readFileArray[0].filter(value => comparePathName.includes(value))
    
    let expectedArray = comparePathName.filter(value => !readFileArray[0].includes(value))
    

    Of course, to get all different values you will need to filter both ways by switching the arrays in the code above, and then combine the results. Or simply check which array's got more elements and use that for the filter.

    Simple example:

    let readFileArray = [
      [
        'mainFolder/abc_doc.txt', 
        'mainFolder/pqr_doc.txt', 
        'mainFolder/subFolder/xyz_image.jpg',
        'mainFolder/subFolder/iop_pdf.pdf',
      ],
      [
        'Fish',
        'Life',
        'Qwerty',
        'Moon'
      ]
    ]
    
    let comparePathName = [
      'mainFolder/abc_doc.txt',
      'mainFolder/pqr_doc.txt',
      'mainFolder/subFolder/xyz_image.jpg', 
      'mainFolder/subFolder/iop_pdf.pdf', 
      'mainFolder/fish.txt', 
      'mainFolder/life.txt',
      'mainFolder/subFolder/qwerty.jpg', 
      'mainFolder/subFolder/moon.pdf', 
    ]
    
    let availableName = readFileArray[0].filter(value => comparePathName.includes(value))
    let expectedArray = comparePathName.filter(value => !readFileArray[0].includes(value))
    
    console.log('Common', availableName)
    console.log('Different', expectedArray)