簡體   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'}
 ]

在上面的示例中,如何通過 name:'b3' 的值獲取第二個對象? 名稱是產品數組的屬性,產品是對象數組的屬性。 如果有人可以幫助我,我將不勝感激。

要根據產品列表的“名稱”屬性搜索項目,您可以執行以下操作:

 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