I'm trying to create a function that will return the needed attribute from an object.
The object will look like this:
export var Characters = [
{
id: 1,
Name: "Abe",
HitPointValue: "124",
StrengthValue: "12",
IntelligenceValue: "14",
WisdomValue: "16",
DexterityValue: "12",
ConstitutionValue: "10",
CharismaValue: "17",
Avatar: require('./images/avatar_7.jpg')
}
]
I tried this:
export function getStat(id, stat) {
var idx = Characters.findIndex((val) => val.id == id);
return Characters[idx].stat;
}
For example, let's say I need to get the "WisdomValue" of this object.
So I call it like this:
<Text style={[styles.stats]}>
{"\n"}
Wisdom Value: {getStat(1, 'WisdomValue')}{"\n"}
</Text>
But I just get an error 'undefined is not an object'
How can I get just one specific attribute, but in a dynamic way? So I don't have to write a separte function like, getHitPointValue(id), get StrengthValue(id), etc...
thanks!