Create array of objects based by filtering the data based on another array

I have a array of objects as below

const history = [
  {
    name: "Lance",
    baseline: { risk: "112", age: "45", insurance: "Star" },
  },
  {
    name: "Sam",
    baseline: { risk: "9", age: "21", insurance: "Sigma" },
  },
  {
    name: "Jill",
    baseline: { risk: "15", age: "21", insurance: "Sigma" },
  },
  {
    name: "Bill",
    baseline: { risk: "15", age: "21", insurance: "Mercy" },
  },
];

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

  {
    name: "Jill",
    item: "PS",
    "3-Aug-21": "10.14",
    "4-Aug-21": "10.2",
    "5-Aug-21": "10",
  },
  {
    name: "Bill",
    item: "PS",
    "3-Aug-21": "10.14",
    "4-Aug-21": "10.2",
    "5-Aug-21": "10",
  },
];

Can anyone get the output as below.

const filteredData= [
{
  insurance: 'Star',
  details:
  {
    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)
    ]
  }},
  {insurance: 'Sigma'
  ,
  details:
  {
    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)
    ],
name: "Jill",
    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)
    ],
  },
},
{insurance: 'Mercy',
details:
  {
    name: "Bill",
    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)
    ],
  }
},
];

I got a similar questions answered. Link –> Using an external object in array map() method

The difference in this question is that here I am trying to read the insurance from the history array and then get the values for patients of the same insurance together.

Based on the previous question. I have the Map between name & insurance with the code below.

const historyMap = new Map(history.map(o => [o.name, o.baseline.insurance]));

However, I do not know how to use it further.

I got the code below where they categorize stuff based on the ‘coins’ (in our case insurance & data is also structured differenly) but I am not able to figure it out.

filteredData = {};
  Object.keys(data).forEach((coin) => {
    filteredData[coin] = data[coin]
      .filter((d) => {
        return !(d["price_usd"] == null);
      })
      .map((d) => {
        d["price_usd"] = Number(d["price_usd"]);
        d["24h_vol"] = Number(d["24h_vol"]);
        d["market_cap"] = Number(d["market_cap"]);
        d["date"] = parseTime(d["date"]);
        return d;
      });
  });

Answer

You could take a two loops approach with objects as hash tables.

const
    history = [{ name: "Lance", baseline: { risk: "112", age: "45", insurance: "Star" } }, { name: "Sam", baseline: { risk: "9", age: "21", insurance: "Sigma" } }, { name: "Jill", baseline: { risk: "15", age: "21", insurance: "Sigma" } }, { name: "Bill", baseline: { risk: "15", age: "21", insurance: "Mercy" } }],
    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" }, { name: "Jill", item: "PS", "3-Aug-21": "10.14", "4-Aug-21": "10.2", "5-Aug-21": "10" }, { name: "Bill", item: "PS", "3-Aug-21": "10.14", "4-Aug-21": "10.2", "5-Aug-21": "10" }],
    temp = current.reduce((r, { name, item, ...data }) => {
        r[name] = Object
            .entries(data)
            .map(([date, riskLevel]) => ({ date, riskLevel }));
        return r;
    }, {}),
    result = Object.values(history.reduce((r, { name, baseline: { risk, insurance } }) => {
        r[insurance] ??= { insurance, details: [] };
        r[insurance].details.push({
            name,
            values: temp[name].map(({ date, riskLevel }) =>
                ({ date, riskLevel: (100 * riskLevel / risk).toFixed(1) }))
        });
        return r;
    }, {}));
  
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }