FormGroup FormArray-按名称仅删除一个元素对象。角度的

时间:2018-12-06 15:33:10

标签: javascript json angular formarray

).    

我想在发送表单POST请求之前从formGroup中删除projectName。可能吗?我尝试使用重置方法,但这会删除所有元素 https://stackblitz.com/edit/generar-json?file=app%2Fapp.component.ts

1 个答案:

答案 0 :(得分:0)

基本上,您需要的是一张地图,以获取没有选定选项的新Array

let object = {
  "companies": [{
    "company": "example comany",
    "projects": [{
        "projectName": "example project",
        "numero": ""
      },
      {
        "projectName": "example project",
        "numero": ""
      },
      {
        "projectName": "example project",
        "numero": ""
      },
      {
        "projectName": "example project",
        "numero": "3"
      }
    ]
  }]
}

object.companies = object.companies.map(company => {
  company.projects = company.projects.map(project => {
    delete project['projectName']; // we delete the key on project each.
    return project;
  });
  return company
});
console.log(object)