Reputation: 3272
I am fairly new to react and 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
I want it to return values like 1.22 and 0.88
Upvotes: 0
Views: 61
Reputation: 3115
Actually, you don't need lodash for that, you could use the Array.prototype.map
function:
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:
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_.round
when there is also a toFixed
Upvotes: 3
Reputation: 1596
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....
Upvotes: 1