JS map return object Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of JS map return object without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I got this array,

var rockets = [
    { country:'Russia', launches:32 },
    { country:'US', launches:23 },
    { country:'China', launches:16 },
    { country:'Europe(ESA)', launches:7 },
    { country:'India', launches:4 },
    { country:'Japan', launches:3 }
];

What do I need to do to return an array mapped, that adds 10 to each

launches

value, here my first approach,

var launchOptimistic = rockets.map(function(elem){
  // return  elem.launches+10;
     return (elem.country, elem.launches+10);


});
console.log(launchOptimistic);

Answer

Use .map without return in simple way. Also start using let and const instead of var because let and const is more recommended

const rockets = [
    { country:'Russia', launches:32 },
    { country:'US', launches:23 },
    { country:'China', launches:16 },
    { country:'Europe(ESA)', launches:7 },
    { country:'India', launches:4 },
    { country:'Japan', launches:3 }
];

const launchOptimistic = rockets.map(elem => (
  {
    country: elem.country,
    launches: elem.launches+10
  } 
));

console.log(launchOptimistic);
We are here to answer your question about JS map return object - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji