Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript find in nested array

const products = [
  { id: 01,
    items: [ { id: 01, name: 'apple' },
             { id: 02, name: 'banana'},
             { id: 03, name: 'orange'}]},
  { id: 02,
    items: [ { id: 01, name: 'carrot' },
             { id: 02, name: 'lettuce'},
             { id: 03, name: 'peas'   }]},
  { id: 03,
    items: [ { id: 01, name: 'eggs'  },
             { id: 02, name: 'bread' },
             { id: 03, name: 'milk'  }]}
 ]

    let found;
    for ( const category of products ){
        found = category.items.find( item => item.name == "milk" )
        if ( found ) break
    }
    console.log( found ) 
    // == { id: 3, name: 'milk' }
 Run code snippet
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #javascript #find #nested #array
ADD COMMENT
Topic
Name
9+2 =