简体   繁体   English

过滤对象数组的对象

[英]filter an object of an array of an object

 Objects:[
  {id:'a',
   owner: 'b',
   products:[{productId:'a1',name:'b1'},{productId:'a2',name:'b2'}]
   date: '2-2'},

  {id:'e',
   owner: 'f',
   products:[{productId:'a3',name:'b3'},{productId:'a4',name:'b4'}]
   date: '1-1'}
 ]

In above example, how do I get the second object by the value of name:'b3'?在上面的示例中,如何通过 name:'b3' 的值获取第二个对象? The name is an attribute of products array and products is an attribute of objects array.名称是产品数组的属性,产品是对象数组的属性。 I'll appreciate if someone can help me out.如果有人可以帮助我,我将不胜感激。

To search for items based on the "name" property of the products list, you can do the following:要根据产品列表的“名称”属性搜索项目,您可以执行以下操作:

 const Objects = [ {id:'a', owner: 'b', products:[{productId:'a1',name:'b1'},{productId:'a2',name:'b2'}], date: '2-2'}, {id:'e', owner: 'f', products:[{productId:'a3',name:'b3'},{productId:'a4',name:'b4'}], date: '1-1' } ] const hasProductsByName = (products) => { return products.some(product => product.name == searchValue); } const searchValue = "b3"; const result = Objects.filter(obj => hasProductsByName(obj.products)); console.log(result);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM