Return array value with forEach()

15,508

try map instead of foreach

req.body.items.map(function(item){ return item.name; })
Share:
15,508
Admin
Author by

Admin

Updated on June 13, 2022

Comments

  • Admin
    Admin almost 2 years

    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.

  • Admin
    Admin almost 8 years
    Great! It works! Thanks!
  • asdf_enel_hak
    asdf_enel_hak almost 8 years
    Glad to help, good luck
  • KANJICODER
    KANJICODER almost 6 years
    This helped me as well, not sure why it had 0 votes when I got to it.