遍历对象数组并获得新的对象数组
Iterate through array of objects and obtain a new array of object
我有以下对象数组,每个对象都有一个 projects
属性,该属性还有它的对象数组。
const data = [
{
"title": "Release",
"projects": [
{
"name": "Server",
"result": {
"success": 0,
"failure": 100
},
}
]
},
{
"title": "Payments",
"projects": [
{
"name": "Platform1",
"result": {
"success": 100,
"failure": 0
}
},
{
"name": "Platform2",
"result": {
"success": 50,
"failure": 50,
}
}
]
}
]
我想遍历它并得到如下结果。 name
只不过是上述数据中 title
和 name
的串联。
const result = [
{
name: 'Release-Server',
success: 0,
failure: 100,
},
{
name: 'Payments-Platform1',
success: 100,
failure: 0,
},
{
name: 'Payments-Platform2',
success: 50,
failure: 5,
},
];
我尝试了以下方法,但无法弄清楚如何准确地获得如上所示的结果。有人可以帮忙吗。
data.forEach(prj => {
prj.projects.forEach((project) => {
// unable to get how to push these details into a new array of object
})
});
您可以执行以下操作(确保添加对 null/undefined 引用的检查)
const data = [{
"title": "Release",
"projects": [{
"name": "Server",
"result": {
"success": 0,
"failure": 100
},
}]
},
{
"title": "Payments",
"projects": [{
"name": "Platform1",
"result": {
"success": 100,
"failure": 0
}
},
{
"name": "Platform2",
"result": {
"success": 50,
"failure": 50,
}
}
]
}
];
const result = data.flatMap(item =>
item.projects.map(project => ({
name: `${item.title}-${project.name}`,
success: project.result.success,
failure: project.result.failure
})));
console.log(result);
你应该首先使用 flatMap
因为你会改变元素的数量(你从 2 开始,你以 3 结束)并且在一个简单的 map
中转换 1:1每个项目都变成你的最终对象。
工作演示
const data = [{
"title": "Release",
"projects": [{
"name": "Server",
"result": {
"success": 0,
"failure": 100
},
}]
},
{
"title": "Payments",
"projects": [{
"name": "Platform1",
"result": {
"success": 100,
"failure": 0
}
},
{
"name": "Platform2",
"result": {
"success": 50,
"failure": 50,
}
}
]
}
];
var groupedData = data.flatMap((el) =>
el.projects.map((proj) => ({
name: el.title + "-" + proj.name,
success: proj.result.success,
failure: proj.result.failure,
}))
);
console.log(groupedData);
我有以下对象数组,每个对象都有一个 projects
属性,该属性还有它的对象数组。
const data = [
{
"title": "Release",
"projects": [
{
"name": "Server",
"result": {
"success": 0,
"failure": 100
},
}
]
},
{
"title": "Payments",
"projects": [
{
"name": "Platform1",
"result": {
"success": 100,
"failure": 0
}
},
{
"name": "Platform2",
"result": {
"success": 50,
"failure": 50,
}
}
]
}
]
我想遍历它并得到如下结果。 name
只不过是上述数据中 title
和 name
的串联。
const result = [
{
name: 'Release-Server',
success: 0,
failure: 100,
},
{
name: 'Payments-Platform1',
success: 100,
failure: 0,
},
{
name: 'Payments-Platform2',
success: 50,
failure: 5,
},
];
我尝试了以下方法,但无法弄清楚如何准确地获得如上所示的结果。有人可以帮忙吗。
data.forEach(prj => {
prj.projects.forEach((project) => {
// unable to get how to push these details into a new array of object
})
});
您可以执行以下操作(确保添加对 null/undefined 引用的检查)
const data = [{
"title": "Release",
"projects": [{
"name": "Server",
"result": {
"success": 0,
"failure": 100
},
}]
},
{
"title": "Payments",
"projects": [{
"name": "Platform1",
"result": {
"success": 100,
"failure": 0
}
},
{
"name": "Platform2",
"result": {
"success": 50,
"failure": 50,
}
}
]
}
];
const result = data.flatMap(item =>
item.projects.map(project => ({
name: `${item.title}-${project.name}`,
success: project.result.success,
failure: project.result.failure
})));
console.log(result);
你应该首先使用 flatMap
因为你会改变元素的数量(你从 2 开始,你以 3 结束)并且在一个简单的 map
中转换 1:1每个项目都变成你的最终对象。
工作演示
const data = [{
"title": "Release",
"projects": [{
"name": "Server",
"result": {
"success": 0,
"failure": 100
},
}]
},
{
"title": "Payments",
"projects": [{
"name": "Platform1",
"result": {
"success": 100,
"failure": 0
}
},
{
"name": "Platform2",
"result": {
"success": 50,
"failure": 50,
}
}
]
}
];
var groupedData = data.flatMap((el) =>
el.projects.map((proj) => ({
name: el.title + "-" + proj.name,
success: proj.result.success,
failure: proj.result.failure,
}))
);
console.log(groupedData);