在下面的数据中。我想让它成为一个以某些值作为键的对象,如下所示:
从这里链接到整个数据 HERE (this.localObject)
const employees = [{
"EmployeeID": "100A",
"FirstName": "Downs",
"aval": [
{"start": "11-19", "end": "2", "ava": "30", "health": "4"},
{"start": "11-20", "end": "2", "ava": "40", "health": "4"},
{"start": "11-21", "end": "2", "ava": "50", "health": "4"},
{"start": "11-22", "end": "2", "ava": "60", "health": "4"}
]
},
{
"EmployeeID": "100B",
"FirstName": "Mckenzie",
"aval": [
{"start": "11-19", "end": "2", "ava": "1", "health": "4"},
{"start": "11-20", "end": "2", "ava": "2", "health": "4"},
{"start": "11-21", "end": "2", "ava": "3", "health": "4"},
{"start": "11-22", "end": "2", "ava": "4", "health": "4"}
]
},
]
到这里:
const employees = [
{ "EmployeeID": "100A", "11-19": "30"},
{ "EmployeeID": "100A", "11-20": "40"},
{ "EmployeeID": "100A", "11-21": "50"},
{ "EmployeeID": "100A", "11-22": "60"},
{ "EmployeeID": "100B", "11-19": "1"},
{ "EmployeeID": "100B", "11-20": "2"},
{ "EmployeeID": "100B", "11-21": "3"},
{ "EmployeeID": "100B", "11-22": "4"}
]
//and so on...
到目前为止我已经尝试过:
this.localData = this.localObject.employees;
const firstLevel = this.localData.map(x => x.aval);
firsLevel 会给我一组更接近我需要的对象,现在我需要将 "start" 值作为键,将 "ava" 在它们自己的对象容器中作为值属性的值
最佳答案
您还需要迭代内部数组并构建单个数组。
var array = [{ EmployeeID: "100A", FirstName: "Downs", aval: [{ start: "11-19", end: "2", ava: "30", health: "4" }, { start: "11-20", end: "2", ava: "40", health: "4" }, { start: "11-21", end: "2", ava: "50", health: "4" }, { start: "11-22", end: "2", ava: "60", health: "4" }] }, { EmployeeID: "100B", FirstName: "Mckenzie", aval: [{ start: "11-19", end: "2", ava: "1", health: "4" }, { start: "11-20", end: "2", ava: "2", health: "4" }, { start: "11-21", end: "2", ava: "3", health: "4" }, { start: "11-22", end: "2", ava: "4", health: "4" }] }],
result = array.reduce((r, { EmployeeID, aval }) => [
...r,
...aval.map(({ start, ava }) => ({ EmployeeID, [start]: ava }))
], []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
关于javascript - 将嵌套数组 -> 数组 -> 对象转换为数组 -> 对象 => 对象值 : object value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53460494/