How to get array of object based on condition in javascript

I have two array objects arr1 and arr2 ,

arr1 property qty should be less than sum of arr1 of stock and arr2 of quantity for same name .

How to return the array object based on comparison of property and same name in javascript

var arr1=[
  {id:1, name: "sample1", qty:2, stock: 0},
  {id:2, name: "sample2", qty:3, stock: 2},
  {id:3, name: "sample2", qty:4, stock: 1}
]

var arr2=[
  {idx:1, name:"sample1", quantity:1},
  {idx:2, name:"sample2", quantity:2},
  {idx:3, name:"sample3", quantity:1}
]

Expected Output,

[{id:1, name: "sample1", qty:2, stock: 0},
  {id:3, name: "sample2", qty:4, stock: 1}]

you can use the filter method to filter stuff

or simply just iterate over the array with a for loop and keep the elements that correspond to your conditions

try it yourself, we can help you if you get stuck

2 Likes

got output , doubt cleared thanks for helping.