-1

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?

trincot
  • 317,000
  • 35
  • 244
  • 286
Rodrigo
  • 49
  • 5

2 Answers2

0

Add the console.log within the function. Also, nome is misspelled.

var returnNames = function(item, index) {
    var p = names[index].name;
    console.log(p);
}
Juan
  • 477
  • 4
  • 8
  • When it takes out the console.log it doesn't print anything. What I want to do is when calling the function to return the names without needing to use console.log. – Rodrigo Dec 30 '21 at 22:59
0

names = [{
    name: "A"
  },
  {
    name: "B"
  },
  {
    name: "C"
  },
  {
    name: "D"
  }
];

var arr = names.map(({name})=>name);
console.log(arr);

Must be the spelling mistake

Ganesh Babu
  • 3,590
  • 11
  • 34
  • 67
  • When it takes out the console.log it doesn't print anything. What I want to do is when calling the function to return the names without needing to use console.log. – Rodrigo Dec 30 '21 at 22:59
  • @Rodrigo Does the updated solution match your expectation? – Ganesh Babu Dec 30 '21 at 23:02