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;
});
});
detailsinfilteredDatashould be array.name:<value>key/value pair inside it.Array.reduce,Array.map,Object destructuringand n number of advanced JavaScript techinques and not a single line explanation provided for the logic that he used. And the best thing is the user stated to Other answes that Assuming the nodes other than name and item in current array as date is not possible since that wont work with dynamic data. The approved answer also uses of the same technique. So what is the reason for the accepting that particular answer?