I have the following code snippet in JS
names = [{name: "A"},
{name: "B"},
{name: "C"},
{name: "D"}];
var returnNames = function(item, index) {
var p = names[index].nome;
return p;
}
names.forEach(returnNames);
I would like to print all the names according to the index but when I call the function it doesn't return any value What am I doing wrong?
Add the console.log within the function. Also, nome is misspelled.
var returnNames = function(item, index) {
var p = names[index].name;
console.log(p);
}
names = [{
name: "A"
},
{
name: "B"
},
{
name: "C"
},
{
name: "D"
}
];
var arr = names.map(({name})=>name);
console.log(arr);
Must be the spelling mistake