javascript array find first object code example
Example 1: js get first object value
var obj = { first: 'someVal' };
obj[Object.keys(obj)[0]]; //returns 'someVal'
Example 2: 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"
}