I've seen a lot of different ways to access values in an array of objects. One being arr.forEach(Function)
in which the function is simply a for...in
But im curious, how come two for...in
functions does not work, for example:
[{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }]
(Taken from freeCodeCamp).
This works:
function myFunction (item, index) {
for( var key in item ) {
console.log(item[key])
}
}
arr.forEach(myFunction
( prints all the values fine)
but how come this does not work:
for(key in arr)
{
for(value in key)
{
console.log(key[value];
}
}
I would think this would work, since we can do arr[key] (and print out the objects in the outer loop, but im not able to access the values this way)
edit:
Sidenote: is it possible for me to print out each sub key/value pair: IE for example on Array index 0 to print out "first: "Romeo"
and then "last: "Montague"