如何将数组中的字段转换为单个对象

时间:2019-05-09 14:18:08

标签: javascript angular

我的反应式数组中有以下值:

ADVERSE_POSESSION: ""
PRIMARY_RESIDENCE: "Yes"
SUBJECT_TO_TENANCY: "Tenancy"

如何遍历这些字段,以便每个字段都是数组中自己的对象?

我想要的输出是:

{
    "fieldId": "ADVERSE_POSESSION",
    "value": ""
},
{
    "fieldId": "PRIMARY_RESIDENCE",
    "value": "Yes"
},
{
    "fieldId": "SUBJECT_TO_TENANCY",
    "value": "Tenancy"
}

1 个答案:

答案 0 :(得分:0)

重新映射它。这是一个例子。

const formValue = {
  ADVERSE_POSESSION: "",
  PRIMARY_RESIDENCE: "Yes",
  SUBJECT_TO_TENANCY: "Tenancy",
}; // Likely this.formGroup.getRawValue()

const array = Object.keys(formValue).map(key => ({ fieldId: key, value: formValue[key] }));
console.log(array);