I have two array of objects:
today = [{strength: 2, health: 4, light: 4}, {strength: 2, health: 3 light: 1}];
tomorrow = [{strength: 1, health: 2, light: 2}, {strength: 4, health: 2 light: 2}];
I am passing these down separately to a component, I get the data fine, and create an object to suit my needs:
const data = today.map((item, index) => {
return {
todayStrength: item.strength,
tomorrowStrength: tomorrow[index].strength
}
})
Now, this would give me the right values within the new object. But I would like my hardcoded strength
key to be dynamic, so that passedInProp
could be either "strength", "health" or "light".
So I can then access my two objects properties based on the value that is being passed on into my component.
That is how I've tried to use the dynamic prop to get the data, but failed:
const data = today.map((item, index) => {
return {
todayStrength: item.passedInProp,
tomorrowStrength: tomorrow[index].passedInProp
// or tomorrowStrength: tomorrow[index][passedInProp]
}
})