0

I am having two arrays I need to check if Array(A) contains all the elements of Array(B) but Array(A) can have more elements than Array(B) I have tried with the below code but it is not working as expected.

Below is my code

let ArrayA = [{headerName:'No'},{headerName:'zahid'},{headerName:'hussain'}];
let ArrayB = [{headerName:'zahid'},{headerName:'abass'},{headerName:'hussain'}];

checkArrayElements() {
    let value: boolean;
    this.ArrayA.map((element) => {
        value =
            this.ArrayB.find(
                (field: any) => field.headerName == element.headerName
            ) != null;
    });
    return value;
}

could anyone please tell me where I am making the mistake?

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
Zahid Hussain
  • 981
  • 2
  • 13
  • 28
  • So the expected result for the above should be `false`? Since `{headerName:'abass'}` is missing from A – Nick Parsons Mar 06 '21 at 05:50
  • @NickParsons yes – Zahid Hussain Mar 06 '21 at 05:55
  • Does this answer your question? [Determining whether one array contains the contents of another array in JavaScript/CoffeeScript](https://stackoverflow.com/questions/15514907/determining-whether-one-array-contains-the-contents-of-another-array-in-javascri) – A.T. Mar 06 '21 at 06:02

4 Answers4

3

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);
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
0

Using filter (a simplified option)

const value =
      this.ArrayB.filter(f => this.ArrayA.map(m => m.headerName).includes(f.headerName)).length === this.ArrayB.length;
    console.log(value);
Zunayed Shahriar
  • 2,557
  • 2
  • 12
  • 15
0

You can get diffrences between two arrays like this:

let arrayA = [
    { headerName: "No" },
    { headerName: "zahid" },
    { headerName: "hussain" },
  ];
  let arrayB = [
    { headerName: "zahid" },
    { headerName: "abass" },
    { headerName: "hussain" },
  ];

  let firstArray = [];
  let secondArray = [];
  let diffrence = [];
  arrayA.map((objects) => {
    Object.keys(objects).map((key) => firstArray.push(objects[key]));
  });
  arrayB.map((objects) => {
    Object.keys(objects).map((key) => secondArray.push(objects[key]));
  });

  firstArray.map((element) => {
    if (!secondArray.includes(element)) {
      diffrence.push(element);
    }
  });
  secondArray.map((element) => {
    if (!firstArray.includes(element)) {
      diffrence.push(element);
    }
  });

  console.log(diffrence);
Arda Örkin
  • 121
  • 3
  • 8
0

Can work around this simple implementation:

let ArrayA = [{headerName: 'No'}, {headerName: 'zahid'}, {headerName: 'hussain'}];
let ArrayB = [{headerName: 'zahid'}, {headerName: 'abass'}, {headerName: 'hussain'}];

let checkArrayElements = () => {
  let allPresent = true;
  for (let i = 0; i < ArrayB.length; i++) {

    allPresent = ArrayA.map(a => a.headerName).indexOf(ArrayB[i].headerName) !== -1;
    if (!allPresent)
      break;
  }
  return allPresent;
};
console.log(checkArrayElements());
Parth Mansata
  • 145
  • 1
  • 7