I am trying to change my array of object values with my object values through the key
let a = [
{
title: "rewardValue",
value: "1",
},
{
title: "rewardValue2",
value: "10",
},
{
title: "rewardValue2",
value: "12",
},
];
let aofValue = {
rewardValue: "200",
rewardValue2: "500",
rewardValue3: "800"
};
a.map((val,index) => {
if(val.title === Object.keys(aofValue)[0]) {
val.value = aofValue.rewardValue
}
if (val.title === Object.keys(aofValue)[1]) {
val.value = aofValue.rewardValue2;
}
if (val.title === Object.keys(aofValue)[2]) {
val.value = aofValue.rewardValue3;
}
})
console.log(a);
I got what I expected but is there any shorted way to do this ´?
I think the way which I done is completely wrong but it return what I expected any other possible solution for this and if available kindly explain the solution
3
Answers
Directly use
Array#map
, replacing the value with the property value for the title in the object (accessed with bracket notation).Try like below:
A possible solution:
This solution takes into account the fact that title of array
a
may not be present inaofValue