Re-arranging an array to have keys have values(重新排列数组以使键具有值)
本文介绍了重新排列数组以使键具有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Javascript对象数组,如下所示
const lab = [
{
name: "Lance",
item: "PS",
"3-Aug-21": "117.69",
"4-Aug-21": "120.1",
"5-Aug-21": "122.08",
},
{
name: "Sam",
item: "PS",
"3-Aug-21": "100.14",
"4-Aug-21": "101.2",
"5-Aug-21": "103",
},
{
name: "Vince",
item: "PS",
"3-Aug-21": "12.65",
"4-Aug-21": "13.29",
"5-Aug-21": "13.68",
},
];
我将其修改如下
const labCheck = [
{
id: "Lance",
values: [
{ date: "3-Aug-21", risklevel: "117.69" },
{ date: "4-Aug-21", risklevel: "120.1" },
{ date: "5-Aug-21", risklevel: "122.08" },
],
},
{
id: "Sam",
values: [
{ date: "3-Aug-21", risklevel: "100.14" },
{ date: "4-Aug-21", risklevel: "101.2" },
{ date: "5-Aug-21", risklevel: "103" },
],
},
{
id: "Vince",
values: [
{ date: "3-Aug-21", risklevel: "12.65" },
{ date: "4-Aug-21", risklevel: "13.29" },
{ date: "5-Aug-21", risklevel: "13.68" },
],
},
];
我已经尝试了下面的代码
const labCheck = lab
.map((d) => d.name)
.map(function (name) {
// console.log(id);
return {
id: name,
values: Object.keys(lab[0])
.slice(2)
.map(function (d, id) {
return {
date: d,
risklevel: +lab[id][d],
};
}),
};
});
它似乎起作用了,我得到了结构,但是riskLevel
的值对于所有id
都是相同的,这是不正确的。
我已经试着纠正这个问题好几天了,但是没有成功。 有谁能帮忙吗?
推荐答案
使用destructuring在重构的每个级别隔离并重命名我们感兴趣的属性。
const lab = [{ name: "Lance", item: "PS", "3-Aug-21": "117.69", "4-Aug-21": "120.1", "5-Aug-21": "122.08", }, { name: "Sam", item: "PS", "3-Aug-21": "100.14", "4-Aug-21": "101.2", "5-Aug-21": "103", }, { name: "Vince", item: "PS", "3-Aug-21": "12.65", "4-Aug-21": "13.29", "5-Aug-21": "13.68", },];
const labCheck = lab.map(({ name: id, item, ...values }) => ({
id,
values: Object.entries(values).map(([date, risklevel]) => ({ date, risklevel }))
}));
console.log(labCheck)
.as-console-wrapper { max-height: 100% !important; top: 0; }
如果需要在重构过程中变化值,则不能使用property shorthand,而需要显式声明每个属性并分配相关的变化值。
const lab = [{ name: "Lance", item: "PS", "3-Aug-21": "117.69", "4-Aug-21": "120.1", "5-Aug-21": "122.08", }, { name: "Sam", item: "PS", "3-Aug-21": "100.14", "4-Aug-21": "101.2", "5-Aug-21": "103", }, { name: "Vince", item: "PS", "3-Aug-21": "12.65", "4-Aug-21": "13.29", "5-Aug-21": "13.68", },];
const labCheck = lab.map(({ name: id, item, ...values }) => ({
id,
values: Object.entries(values).map(([date, risklevel]) => ({
date: new Date(Date.parse(date)), // Date.parse() is not reliable, but used as example here.
risklevel: +risklevel
}))
}));
console.log(labCheck)
.as-console-wrapper { max-height: 100% !important; top: 0; }
这篇关于重新排列数组以使键具有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:重新排列数组以使键具有值
猜你喜欢
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 400或500级别的HTTP响应 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01