Reputation:
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,
]
Upvotes: 1
Views: 74
Reputation: 5542
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)
Upvotes: 0
Reputation: 30705
We can create functions to get the intersection and difference of the two arrays as below, then we call them by passing the relevant values.
I've decided to use Set.has rather than Array.includes to improve performance (though this would only make a difference for very large arrays)
const readFileArray = [ [ 'mainFolder/abc_doc.txt', 'mainFolder/pqr_doc.txt', 'mainFolder/subFolder/xyz_image.jpg', 'mainFolder/subFolder/iop_pdf.pdf', ], [ 'Fish', 'Life', 'Qwerty', 'Moon' ] ]
const 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', ]
/* Return all the items in one array only, use sets to avoid N squared behaviour */
function getSymmetricDifference(array1, array2) {
const set1 = new Set(array1);
const set2 = new Set(array2);
const itemsInArray1Only = array1.filter(el => !set2.has(el))
const itemsInArray2Only = array2.filter(el => !set1.has(el))
return itemsInArray1Only.concat(itemsInArray2Only)
}
/* Return all common items, use set to avoid N squared behaviour */
function getIntersection(array1, array2) {
const set2 = new Set(array2);
return array1.filter(el => set2.has(el));
}
console.log("Intersection:", getIntersection(readFileArray[0], comparePathName))
console.log("Difference:", getSymmetricDifference(readFileArray[0], comparePathName))
Upvotes: 1