Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

return the first matching object from an array

function priceLookup(items, name) {

  for (let i = 0; i < items.length; i++) {
    if (name === items[i].itemName) {
      // the return will break the loop and exit the function
      return items[i].price;
    }
  }
  // if loop completes no matches were found
  return "No item found with that name"
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #return #matching #object #array
ADD COMMENT
Topic
Name
7+2 =