Answers for "return the first matching object from an array"

0

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"
}
Posted by: Guest on June-10-2021

Code answers related to "return the first matching object from an array"

Browse Popular Code Answers by Language