[英]Selecting array value for data clense
I am fairly new to react and lodash. 我是新来的反应和lodash。 I am trying to get some data rounded to two decimal places. 我正在尝试将一些数据四舍五入到小数点后两位。
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
is coming in as undefined, how do I reference it so it knows to look inside of selectedPlayers
SHOT__Goals
即将进入未定义状态,我该如何引用它,以便知道查看selectedPlayers
内部
I want it to return values like 1.22 and 0.88 我希望它返回1.22和0.88之类的值
Actually, you don't need lodash for that, you could use the Array.prototype.map
function: 实际上,您不需要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))
However, for what I gather you don't really want goals
to be a function but the resulting array if I am right then the following would do: 但是,对于我收集的内容,您并不是真的希望goals
成为一个函数,但是如果我没错,那么结果数组将满足以下要求:
const selectedPlayers = [{ SHOT__Goals: 1.222222 }, { SHOT__Goals: 0.888888 }]; const goals = selectedPlayers.map(player => player.SHOT__Goals.toFixed(2)); console.log(goals)
Something to notice: 注意事项:
selectedPlayers
to an array, it is easier to manipulate 我将selectedPlayers
转换为数组,因此更易于操作 _.round
when there is also a toFixed
我不太明白为什么您使用的_.round
时也有toFixed
in this case... 在这种情况下...
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))
Your example had a lot of issues, which i fixed. 您的示例有很多问题,我已解决。 There are better ways of doing this however.... 有更好的方法可以做到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.