admin管理员组

文章数量:1389956

I work on an angular application and I have to pare 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.

I work on an angular application and I have to pare 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.

Share Improve this question edited Mar 12, 2019 at 10:15 Miroslav Jonas 6,6572 gold badges30 silver badges42 bronze badges asked Mar 12, 2019 at 10:03 user10863293user10863293 8765 gold badges14 silver badges40 bronze badges 6
  • You just want to check whether both are equal or not? – Abdul Basit Commented Mar 12, 2019 at 10:07
  • do you want to pare both id and itemId or just id only? – Sheik Althaf Commented Mar 12, 2019 at 10:08
  • Possible duplicate of How to get the difference between two arrays of objects in JavaScript – Shibon Commented Mar 12, 2019 at 10:08
  • I want to check if the itemId is the same. – user10863293 Commented Mar 12, 2019 at 10:08
  • please check below answer you need to use the foreach – TheParam Commented Mar 12, 2019 at 10:11
 |  Show 1 more ment

7 Answers 7

Reset to default 2

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])
  }
}

You want to iterate over both arrays so you can pare 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.

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

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

your.ponent.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.pareArray()
 }


  pareArray() {

    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!

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

  }



})

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)

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

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

本文标签: javascriptHow to compare 2 arrays of object with different lengthStack Overflow