Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为数据元素选择数组值_Javascript_Lodash - Fatal编程技术网

Javascript 为数据元素选择数组值

Javascript 为数据元素选择数组值,javascript,lodash,Javascript,Lodash,我是一个新的反应和洛达斯。我想把一些数据四舍五入到小数点后两位 selectedPlayers { 0: {SHOT__Goals: 1.222222} 1: {SHOT__Goals: 0.888888} } const goals = ( selectedPlayers ) => { const goal= _.round((selectedPlayers.SHOT__Goals), 2).toFixed(2); return goals; } SHOT\uu Goals未

我是一个新的反应和洛达斯。我想把一些数据四舍五入到小数点后两位

selectedPlayers {
0: {SHOT__Goals: 1.222222}
1: {SHOT__Goals: 0.888888}
}

 const goals = ( selectedPlayers ) => {
 const goal= _.round((selectedPlayers.SHOT__Goals), 2).toFixed(2);
 return goals;
 }
SHOT\uu Goals
未定义,我如何引用它以便它知道如何查看
selectedPlayers

在本例中,我希望它返回1.22和0.88之类的值

const selectedPlayers = {
  0: { SHOT__Goals: 1.222222 },
  1: { SHOT__Goals: 0.888888 }
}

const goals = selectedPlayers => {
  return _.round(selectedPlayers[0].SHOT__Goals, 2).toFixed(2)
}

console.log(goals(selectedPlayers))
你的例子有很多问题,我已经解决了。但是,有更好的方法……在这种情况下

const selectedPlayers = {
  0: { SHOT__Goals: 1.222222 },
  1: { SHOT__Goals: 0.888888 }
}

const goals = selectedPlayers => {
  return _.round(selectedPlayers[0].SHOT__Goals, 2).toFixed(2)
}

console.log(goals(selectedPlayers))

你的例子有很多问题,我已经解决了。但是,有更好的方法可以做到这一点….

实际上,您不需要使用lodash,您可以使用
Array.prototype.map
函数:

const selectedPlayers=[{SHOT_uugoals:1.2222222},{SHOT_uugoals:0.888888}];
const goals=selectedPlayers=>selectedPlayers.map(player=>player.SHOT\u goals.toFixed(2));

console.log(goals(selectedPlayers))
实际上,您不需要lodash,您可以使用
Array.prototype.map
函数:

const selectedPlayers=[{SHOT_uugoals:1.2222222},{SHOT_uugoals:0.888888}];
const goals=selectedPlayers=>selectedPlayers.map(player=>player.SHOT\u goals.toFixed(2));

log(goals(selectedPlayers))
这与react无关。该标记在这里不合适,而且示例中存在语法错误。您可以解决它们吗?这与react无关。这个标记在这里是不合适的,在你的例子中也有语法错误。你能解决它们吗?我建议这样做@汤姆:我建议你这样做@汤姆