繁体   English   中英

选择数据值的数组值

[英]Selecting array value for data clense

我是新来的反应和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__Goals即将进入未定义状态,我该如何引用它,以便知道查看selectedPlayers内部

我希望它返回1.22和0.88之类的值

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

 const selectedPlayers = [{ SHOT__Goals: 1.222222 }, { SHOT__Goals: 0.888888 }]; const goals = selectedPlayers => selectedPlayers.map(player => player.SHOT__Goals.toFixed(2)); console.log(goals(selectedPlayers)) 

但是,对于我收集的内容,您并不是真的希望goals成为一个函数,但是如果我没错,那么结果数组将满足以下要求:

 const selectedPlayers = [{ SHOT__Goals: 1.222222 }, { SHOT__Goals: 0.888888 }]; const goals = selectedPlayers.map(player => player.SHOT__Goals.toFixed(2)); console.log(goals) 

注意事项:

  • 我将selectedPlayers转换为数组,因此更易于操作
  • 我不太明白为什么您使用的_.round时也有toFixed

在这种情况下...

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))

您的示例有很多问题,我已解决。 有更好的方法可以做到这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM