简体   繁体   English

如何根据 javascript 中的条件获取 object 的数组

[英]how to get array of object based on condition in javascript

I have two array objects arr1 and arr2 ,我有两个数组对象arr1arr2

arr1 property qty should be less than sum of arr1 of stock and arr2 of quantity for same name . arr1属性qty应小于相同name的库存 arr1 和数量 arr2 的总和。

How to return the array object based on comparison of property and same name in javascript如何根据 javascript 中的属性和同名比较返回数组 object

function getArray(arr1,arr2)
{
 return arr1.filter(i=> arr2.map(e=>i. qty < (e.quantity+i.stock)))
}
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,预计 Output,

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

try this:尝试这个:

arr1.filter(item1=>item1.stock<(arr2.filter(item2=>item2.name==item1.name))[0].quantity)

will work as long as array2 has unique names.只要 array2 具有唯一的名称,它就可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据角度8中的条件从对象数组中获取最后一个对象 - How to get last object from array of object based on condition in angular 8 如何根据 javascript 中的条件对现有数组 object 中的值求和 - How to sum value in the existing array object based on condition in javascript 如何根据数组对象javascript中的条件检查返回布尔值 - how to return boolean value based on condition check in array object javascript 如何根据数组 object javascript 中的条件删除密钥 - How to remove key based on condition in array object javascript 如何根据JavaScript中的某个条件得到object值? - How to get the object value based on a certain condition in JavaScript? 根据JavaScript中的条件将对象拆分为对象数组 - Split an object into array of objects based on a condition in JavaScript 根据 javascript 中的条件修改现有数组 object - modify the existing array object based on condition in javascript 如何使用条件从javascript中的对象数组中获取值 - how to get values from object array in javascript with condition 如何根据 javascript 中 object 的值获取 object 数组 - How to get the object array based on value of object in javascript 如何根据object和javascript中的数组列表获取对象数组 - How to get array of objects based on object and array list in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM