我有如下所示的对象数组
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" },
},
];然后是第二个,如下所示
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",
},
];有人能得到如下的输出吗?
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)
],
}
},
];我也回答了类似的问题。链接-> Using an external object in array map() method
这个问题的不同之处在于,在这里,我试图从历史数组中读取保险,然后一起获得相同保险的患者的值。
基于前面的问题。我有名称和保险之间的地图,代码如下。
const historyMap = new Map(history.map(o => [o.name, o.baseline.insurance]));然而,我不知道如何进一步使用它。
我得到了下面的代码,他们根据“硬币”对物品进行分类(在我们的例子中,保险&数据也是不同的结构),但我无法弄清楚。
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;
});
});发布于 2021-08-30 09:14:49
您可以采用两个循环方法,对象作为哈希表。
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; }
发布于 2021-08-30 08:48:30
我使用reduce()对保险进行分类,并使用Object.values()提取数据。
注:精度我不知道你的要求。我要让你调整一下。我现在使用toFixed(1)。
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" }, }, ];
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", }, ];
const categorizedInsurance = history.reduce((acc, h) => {
const cur = current.find(c => c.name === h.name);
const insurance = h.baseline.insurance;
const keys = Object.keys(cur).filter(key => key !== "name" && key !== "item");
const values = keys.map(key => ({
date: key,
riskLevel: ((cur[key] / h.baseline.risk) * 100).toFixed(1),
}));
if (acc[insurance]) {
acc[insurance].details.push({
name: h.name,
values: values,
});
} else {
acc[insurance] = {
insurance: insurance,
details: [
{
name: h.name,
values: values,
},
],
};
}
return acc;
}, {});
const output = Object.values(categorizedInsurance);
console.log(output);
发布于 2021-08-30 09:32:55
使用filteredData的语法检查。但是,似乎每个details数组对象中的filteredData节点必须是一个数组,因为有多个以键作为名称的节点。
逻辑
current数组。current数组中的每个对象找到一个来自具有相同名称的history数组的匹配节点。current数组中的每个节点,循环遍历键。除了dates.item以外的键应该是name,一个detail对象,它从历史数组中以name作为匹配的名称,并为每个日期的值加上risk.accumulator array.accumulator数组中是否存在一个具有相同保险名称的节点。如果已经存在节点,则将新创建的详细信息对象推送到该节点的详细信息数组,或者将一个新节点推送到accumulator数组。工作示例
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" },
},
];
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",
},
];
const output = current.reduce((acc, curr) => {
// Find the node from the array `history` having the same name from array `current`
const historyNode = history.find(node => node.name === curr.name);
const details = [];
const detail = {
name: historyNode.name,
values: [],
}
// Loop through each keys in the object `curr`
// If the node is not `name` and `item` then defenitely its a date
Object.keys(curr).forEach((key) => {
if (key !== 'name' && key !== 'item') {
// Push the date values against the name
detail.values.push({
date: key,
risk: (+curr[key] / +historyNode.baseline.risk * 100).toFixed(1)
})
}
})
details.push(detail)
// Find for a node an insurance same as that we found match from the history array.
// If there is a match found, then this details must be pushed to the `details` array of that particular node
const accumulatorNode = acc.find((node) => node.insurance === historyNode.baseline.insurance);
if (accumulatorNode) {
accumulatorNode.details.push(detail);
} else {
// If there is no node existing with same insurance name, then we need to insert a new object to the accumulator
acc.push({
insurance: historyNode.baseline.insurance,
details,
})
}
return acc;
}, []);
console.log(output)
https://stackoverflow.com/questions/68981055
复制相似问题