user10863293
user10863293

Reputation: 876

How to compare 2 arrays of object with different length?

I work on an angular application and I have to compare 2 arrays of object :

ar1 = [{id: 2, itemId: 1},
    {id: 5, itemId: 3},
    {id: 18,itemId: 13},
    {id: 16,itemId: 14}]

ar2 = [{id: 13, itemId: 1},
    {id: 12, itemId: 14}]

I try to do something like this:

for(let i = 0 ; i < this.ar1.length ; i++){
    if(this.ar2[i] != undefined){
        if(this.ar1[i].itemId == this.ar2[i].itemId){
            console.log("in and ==itemId",this.ar2[i])
        }
        else{
            console.log("in and !=itemId",this.ar1[i])
        }
    }
    else{
        console.log("undefined",this.ar1[i])
    }
}

It's return me that :

in and ==itemId {id: 13, itemId: 1}
in and !=itemId {id: 5, itemId: 3}
undefined {id: 18, itemId: 13}
undefined {id: 16, itemId: 14}

I want a function which can say me which object is in the 2 arrays and which object is not in the 2 arrays.

It's not a duplicate cause I don't want to see difference between 2 arrays of object but see if the itemId if the same and get the object with the same itemId.

Upvotes: 3

Views: 4595

Answers (7)

Tushar Walzade
Tushar Walzade

Reputation: 3809

The following snippet will help you to find where your itemId is common -

let ar1 = [{ id: 2, itemId: 1 },
    { id: 5, itemId: 3 },
    { id: 18, itemId: 13 },
    { id: 16, itemId: 14 }
];

let ar2 = [{ id: 13, itemId: 1 },
    { id: 12, itemId: 14 }
];

let checkCommonElements = function(arr1, arr2) {
    arr1.forEach((object1, index1) => {
    	let flag = false;
        arr2.forEach((object2, index2) => {
            if (object1.itemId === object2.itemId) {
            	flag = true;
                console.log("Item present in ar2: ", JSON.stringify(object1));
            }
        });
        if(!flag) {
            console.log("Item not in ar2: ", JSON.stringify(object1));
        }
    });
}

checkCommonElements(ar1, ar2);

Upvotes: -1

Sharan Arumugam
Sharan Arumugam

Reputation: 363

How about .includes()?

ar1 = [
  {id: 2, itemId: 1},
  {id: 5, itemId: 3},
  {id: 18, itemId: 13},
  {id: 16, itemId: 14},
];

ar2 = [
  {id: 2, itemId: 1},
  {id: 5, itemId: 25},
  {id: 18, itemId: 13},
  {id: 22, itemId: 14},
];

intersection = ar1.filter(x => ar2.map(y => y.itemId).includes(x.itemId));
console.log(intersection);

Peace

Upvotes: 1

Syed Mehtab Hassan
Syed Mehtab Hassan

Reputation: 1337

You have t iterate over both array and find it id of arr1 is present in arr2 or not

ar1 = [{
    id: 2,
    itemId: 1
  },
  {
    id: 5,
    itemId: 3
  },
  {
    id: 18,
    itemId: 13
  },
  {
    id: 16,
    itemId: 14
  }
]

ar2 = [{
    id: 13,
    itemId: 1
  },
  {
    id: 12,
    itemId: 14
  }
]

for (let i = 0; i < this.ar1.length; i++) {
  id = false;
  for (let j = 0; j < this.ar2.length; j++) {
    if (this.ar1[i].itemId == this.ar2[j].itemId) {
      id = true
      data = this.ar2[j];
    }

  }
  if (id) {
    console.log("in and ==itemId", data)
  } else {
    console.log("in and !=itemId", this.ar1[i])
  }
}

Upvotes: 2

Nick
Nick

Reputation: 1422

You want to iterate over both arrays so you can compare all of the values of ar1 with all of the values of ar2.

Here is the basic logic to run the code snippet:

let ar1 = [
  {id: 2, itemId: 1},
  {id: 5, itemId: 3},
  {id: 18,itemId: 13},
  {id: 16,itemId: 14}
];

let ar2 = [
  {id: 13, itemId: 1},
  {id: 12, itemId: 14}
];

for (let x = 0; x < ar1.length; x++) {
  for (let y = 0; y < ar2.length; y++) {
    if (ar1[x].itemId === ar2[y].itemId) {
      console.log("id: " + ar1[x].id + ", itemId: " + ar1[x].itemId + " = id: " + ar2[y].id + ", itemId: " + ar2[y].itemId);
    }
  }
}

You can simplify this further by using forEach instead of traditional for loops. You can also change the console.log logic to fit whatever data you are expecting to output.

Upvotes: 2

Lionel B
Lionel B

Reputation: 1174

Just like this :

const ar1 = [
  { id: 2, itemId: 1 },
  { id: 5, itemId: 3 },
  { id: 18, itemId: 13 },
  { id: 16, itemId: 14 },
]

const ar2 = [{ id: 13, itemId: 1 }, { id: 12, itemId: 14 }]
const inTwoArray = []
const notInTwoArray = []
ar1.forEach(el => {
  if (ar2.find(el1 => el1.itemId === el.itemId)) {
    inTwoArray.push(el)
  } else {
    notInTwoArray.push(el)
  }
})

console.log(inTwoArray)
console.log(notInTwoArray)

Upvotes: -1

brk
brk

Reputation: 50291

You can iterate ar1 and use findIndex to check if there exist an object on ar2 which has same itemId

let ar1 = [{
    id: 2,
    itemId: 1
  },
  {
    id: 5,
    itemId: 3
  },
  {
    id: 18,
    itemId: 13
  },
  {
    id: 16,
    itemId: 14
  }
]

let ar2 = [{
    id: 13,
    itemId: 1
  },
  {
    id: 12,
    itemId: 14
  }
]

ar1.forEach(function(item) {

  let findIndexInar2 = ar2.findIndex(function(elem) {
    return item.itemId === elem.itemId;
  })

  if (findIndexInar2 !== -1) {
    console.log('itemId present in ar2')

  } else {
    console.log('itemId not present in ar2')

  }



})

Upvotes: 0

TheParam
TheParam

Reputation: 10541

Instead of using traditional for use foreach which will traverse every element from the array and check with another element in the array.

your.component.ts

export class AppComponent {
  name = 'Angular';

  ar1 = [{ id: 2, itemId: 1 },
  { id: 5, itemId: 3 },
  { id: 18, itemId: 13 },
  { id: 16, itemId: 14 }]

  ar2 = [{ id: 13, itemId: 1 },
  { id: 12, itemId: 14 }]

 constructor () {
   this.compareArray()
 }


  compareArray() {

    this.ar1.forEach( array1Ttem => {

      this.ar2.forEach( array2Item => {

         if(array1Ttem.itemId == array2Item.itemId){
            console.log("in and ==itemId",array1Ttem);
        }
        else{
            console.log("in and !=itemId",array1Ttem);
        }

      })
    })
  }
}

Solution on Stackblitz

Hope this will help!

Upvotes: 0

Related Questions