遍历对象数组并获取新的对象数组

Are*_*rot 3 javascript arrays iteration loops 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,
                }
            }
        ]
    }
]
Run Code Online (Sandbox Code Playgroud)

我想迭代它并得到如下结果。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,
    },
];
Run Code Online (Sandbox Code Playgroud)

我尝试了以下方法,但无法弄清楚如何获得如上所示的准确结果。有人可以帮忙吗?

data.forEach(prj => {
        prj.projects.forEach((project) => {
          // unable to get how to push these details into a new array of object
        })
      });
Run Code Online (Sandbox Code Playgroud)

cal*_*ack 6

您可以执行以下操作(确保添加对空/未定义引用的检查)

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);
Run Code Online (Sandbox Code Playgroud)