10

I am looking to return the name value of an array stored in another Object.

exports.send = function(req,res){
  req.body.items.forEach(function(item){console.log(item.name)})
}

Console:

Product 3
Product 2

But I don't know how to obtain those values for an HTML content which I want to send it to an email address. I tried with return item.name instead of console.log(item.name) but it doesn't work. Thanks a lot in advance! Newbie out.

  • You use the value where it has been delivered to you. The behavior is asynchronous, so you bring your code to the result instead of the result being brought to your code. –  Jul 03 '16 at 16:25
  • @squint it seems issue is not case poited – asdf_enel_hak Jul 03 '16 at 17:10
  • @asdf_enel_hak: Could be. Trouble is that the OP didn't explicitly show which call site needs to use the attempted return value, nor that a collection of all the `item.name` was desired. I see your accepted answer, but I'm satisfied leaving it closed until the OP provides an edit that shows more precisely the result that is needed. If the OP does actually want to create a new array of `item.name`, then an edit is needed, and the question can be closed as a duplicate of this one instead: http://stackoverflow.com/questions/19590865/from-an-array-of-objects-extract-value-of-a-property-as-array –  Jul 03 '16 at 17:33
  • @asdf_enel_hak: It is indeed the same. Thank you for your answers! –  Jul 04 '16 at 13:07

1 Answers1

24

try map instead of foreach

req.body.items.map(function(item){ return item.name; })
asdf_enel_hak
  • 7,474
  • 5
  • 42
  • 84