I want to access the value of a key:value set within an object via a function. My understanding is that I can use a dot operator to do this. I begin with a simple for loop to console log the set:
const groceries = arr => {
for (let i=0; i<arr.length; i++){
console.log(arr[i]);
}
}
let groceryItems = [{item: ‘Carrots’}, {item: ‘Hummus’}, {item: ‘Pesto’}, {item: ‘Rigatoni’}];
groceries(groceryItems);
//PRINTS:
{ item: ‘Carrots’ }
{ item: ‘Hummus’ }
{ item: ‘Pesto’ }
{ item: ‘Rigatoni’ }
But I only want to access the values (Carrots, Hummus, Pesto, Rigatoni).
My thinking is that I use the dot operator to access these within my function:
const groceries = arr => {
for (let i=0; i<arr.length; i++){
console.log(arr.item[i]);
}
}
This throws up a typeError: Cannot read property ‘0’ of undefined
at groceries.
Can someone explain to me what I’m doing wrong?
Or how I can access the values in this object and print them to the console
Many thanks
S