-1

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.

4
  • 2
    And the problem is? Just add the calculcation of riskLevel into the answer of your linked question. Commented Aug 21, 2021 at 12:45
  • What have you tried so far to solve this on your own? Commented Aug 21, 2021 at 12:46
  • But how to pass-in the history into the map() method which is on the current array? 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 the history array based on the name of the person. Commented Aug 21, 2021 at 12:48
  • The second parameter the .map() callback is an index. Assuming the indexes match you can use .map((item, index) => ... history[index].baseline.risk ... ) Commented Aug 21, 2021 at 13:28

2 Answers 2

1

Note the optional chaining in case there is no item matching the history

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",  },
];

const history = [ 
{ name: "Lance", baseline: { risk: "112", age: "45" }, },
{ name: "Sam",   baseline: { risk: "9", age: "21"   }, },
];

const labCheck = current.map(({ name: id, item, ...values }) => {
  const baseline = history.filter(item => item.name===id)[0]?.baseline.risk ?? 1
  console.log(baseline)
  return {
  id,
  values: Object.entries(values).map(([date, risklevel]) => ({ date, risklevel: parseInt((+risklevel/+baseline)*100) }))
}
});

console.log(labCheck)
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

1 Comment

sorry for the confusion. In my expected output I converted the +risklevel / +baseline into percentage, hence the issue in you results. However, the method you provided is perfect. I changes to +risklevel / +baseline to (+risklevel / +baseline) * 100 and it works fine for me (I get the result I was expecting). TIL, that we can indeed pass another array into the map() method.
1

You could first build a Map that maps each name from history to its risk:

Lance -> 112
Sam -> 9

Once you have the Map, you can get the persons risk by using .get() on the current name, and perforrm your calculation with that risk:

const history = [ { name: "Lance", baseline: { risk: "112", age: "45" }, }, { name: "Sam", baseline: { risk: "9", age: "21" }, }, ];

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", }, ];

const historyMap = new Map(history.map(o => [o.name, o.baseline.risk]));
const res = current.map(({name, item, ...rest}) => ({
  name,
  values: Object.entries(rest).map(([date, risk]) => ({
    date,
    riskLevel: ''+Math.floor((risk / historyMap.get(name)) * 1000) / 10
  }))
}));

console.log(res);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.