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?

3

4 Answers 4

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);

Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

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);

Comments

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());

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.