Javascript JS 地图返回对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47841899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JS map return object
提问by manuelBetancurt
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
我需要做什么才能返回映射的数组,每个数组增加 10
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);
回答by CRice
You're very close already, you just need to return the new object that you want. In this case, the same one except with the launches value incremented by 10:
你已经非常接近了,你只需要返回你想要的新对象。在这种情况下,除了 launch 值增加了 10 之外,是相同的:
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 }
];
var launchOptimistic = rockets.map(function(elem) {
return {
country: elem.country,
launches: elem.launches+10,
}
});
console.log(launchOptimistic);
回答by Hemadri Dasari
Use .map without return in simple way. Also start using let and const instead of var because let and const is more recommended
以简单的方式使用 .map 而不返回。也开始使用 let 和 const 而不是 var 因为更推荐使用 let 和 const
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);
回答by alfasin
maprockets and add 10 to its launches:
map火箭,并在发射时增加 10:
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 }
];
rockets.map((itm) => {
itm.launches += 10
return itm
})
console.log(rockets)
If you don't want to modify rocketsyou can do:
如果你不想修改,rockets你可以这样做:
var plusTen = []
rockets.forEach((itm) => {
plusTen.push({'country': itm.country, 'launches': itm.launches + 10})
})
回答by ibrahim mahrir
If you want to alter the original objects, then a simple Array#forEachwill do:
如果你想改变原始对象,那么一个简单的方法Array#forEach就可以了:
rockets.forEach(function(rocket) {
rocket.launches += 10;
});
If you want to keep the original objects unaltered, then use Array#mapand copy the objects using Object#assign:
如果要保持原始对象不变,请使用Array#map并使用Object#assign以下方法复制对象:
var newRockets = rockets.forEach(function(rocket) {
var newRocket = Object.assign({}, rocket);
newRocket.launches += 10;
return newRocket;
});

