I have a array of objects as below
const history = [
{
name: "Lance",
baseline: { risk: "112", age: "45" },
},
{
name: "Sam",
baseline: { risk: "9", age: "21" },
},
];
Then a second one as below
const current = [
{
name: "Lance",
item: "PS",
"3-Aug-21": "117",
"4-Aug-21": "120",
"5-Aug-21": "112",
},
{
name: "Sam",
item: "PS",
"3-Aug-21": "10.14",
"4-Aug-21": "10.2",
"5-Aug-21": "10",
},
];
Can anyone help getting the output as below
const currentRiskRating = [
{
name: "Lance",
values: [
{ date: "3-Aug-21", riskLevel: "104.4" }, //117/112(112 is the baseline risk for Lance)
{ date: "4-Aug-21", riskLevel: "107.1" }, //120/112(112 is the baseline risk for Lance)
{ date: "5-Aug-21", riskLevel: "100" }, //112/112(112 is the baseline risk for Lance)
],
},
{
name: "Sam",
values: [
{ date: "3-Aug-21", riskLevel: "112.6" }, //10.14/9(9 is the baseline risk for Sam)
{ date: "4-Aug-21", riskLevel: "113.3" }, //10.2/9(9 is the baseline risk for Sam)
{ date: "5-Aug-21", riskLevel: "111.1" }, //10/9(9 is the baseline risk for Sam)
],
},
];
I got my question yesterday where the value in the array of objects are rearranged. Link --> Re-arranging an array to have keys have values
The difference in this question is that here I am trying to read the risk from the history array and divide the values for the dates (I have added the comment where I show how the values is arrived at). Essentially we find the risk in the baseline and then divide the values for each date with the value. Please let me know how I can accomplish this.
I am unable to pass an external array into map() method.
riskLevelinto the answer of your linked question.historyinto the map() method which is on thecurrentarray? in the map() method of one array, can I pass-in the values from another array? I do not know the risk of each person, it has to be searched from thehistoryarray based on the name of the person..map()callback is an index. Assuming the indexes match you can use.map((item, index) => ... history[index].baseline.risk ... )