Answers for "how to grab first object in array of objects javascript"

8

js get first object value

var obj = { first: 'someVal' };
obj[Object.keys(obj)[0]]; //returns 'someVal'
Posted by: Guest on November-03-2020
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 "how to grab first object in array of objects javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language