The main issue with your current approach is that you are re setting value
for every iteration of .map()
(side note: this should be .forEach()
and not .map()
since you are not using the result returned by .map()
- more info here). You are currently only checking if the value of the last element in ArrayA
appears in ArrayB
as you are not taking into account what the previous values on value
were. Instead, you can make your outer loop over array B and then use an inner loop to check if the value from array B appears in A.
With that being said, I think I would use a Set
, where you can merge both array elements and remove the duplicates using a Set. The size of the Set should equal the size of ArrayA if there all elements from B appear in ArrayA:
const ArrayA = [{headerName:'No'},{headerName:'zahid'},{headerName:'hussain'}];
const ArrayB = [{headerName:'zahid'},{headerName:'abass'},{headerName:'hussain'}];
const allInBAppearInA = new Set([...ArrayA, ...ArrayB].map(({headerName}) => headerName)).size === ArrayA.length;
console.log(allInBAppearInA);
If I was to use a loop approach like explained above, I would use .every()
to check that every elements from ArrayB can be found in ArrayA
(you can use .some()
to check that ArrayA contains an object with a given header name matching one from ArrayA
):
const ArrayA = [{headerName:'No'},{headerName:'zahid'},{headerName:'hussain'}];
const ArrayB = [{headerName:'zahid'},{headerName:'abass'},{headerName:'hussain'}];
const res = ArrayB.every(({headerName}) => ArrayA.some(obj => obj.headerName === headerName));
console.log(res);