如何将键值对添加到对象

时间:2020-04-03 14:39:27

标签: javascript angular typescript

我有一个包含嵌套对象数组的数据源。 我已经能够选择键值对,现在我想将这些值添加到对象的顶层,即在嵌套对象之外。

初始数组:

 data= [
  {
    "flowId": 7079,
    "flowName": "jackson-demo",
    "version": 1,
    "CreatedDate": "2020-04-02",
    "UpdateDate": "",
    "LastRunDate": "2020-04-02",
    "active": false,

"properties": [
  {
    "id": 7080,
    "key": "country",
    "value": "in",
    "category": "General"
  },
  {
    "id": 7081,
    "key": "source",
    "value": "hive",
    "category": "General"
  }
  ]

  },
  {

"flowId": 7079,
"flowName": "jackson-demo",
"version": 1,
"CreatedDate": "2020-04-02",
"UpdateDate": "",
"LastRunDate": "2020-04-02",
"active": false,

"properties": [
  {
    "id": 7080,
    "key": "country",
    "value": "au",
    "category": "General"
  },
  {
    "id": 7081,
    "key": "source",
    "value": "aws",
    "category": "General"
  }
  ]

} ]

使用以下代码,我可以获得键值对:

 for (var i = 0; i < data.length; i++) {
  data[i].properties.forEach((arrayItem, i) => {
    if (arrayItem.key === 'country') {
      console.log('Key: ' + arrayItem.key + ' ' + 'Value: ' + arrayItem.value);
    }
  });
}

代码输出:

Key: country Value: au 
Key: country Value: in 

如何将这些值推回数组中,以便新数组如下所示:

data= [
  {
    "flowId": 7079,
    "flowName": "jackson-demo",
    "version": 1,
    "CreatedDate": "2020-04-02",
    "UpdateDate": "",
    "LastRunDate": "2020-04-02",
    "active": false,
    "country": "in"

"properties": [
  {
    "id": 7080,
    "key": "country",
    "value": "in",
    "category": "General"
  },
  {
    "id": 7081,
    "key": "source",
    "value": "hive",
    "category": "General"
  }
  ]

  },
  {

"flowId": 7079,
"flowName": "jackson-demo",
"version": 1,
"CreatedDate": "2020-04-02",
"UpdateDate": "",
"LastRunDate": "2020-04-02",
"active": false,
"country":"au"

"properties": [
  {
    "id": 7080,
    "key": "country",
    "value": "au",
    "category": "General"
  },
  {
    "id": 7081,
    "key": "source",
    "value": "aws",
    "category": "General"
  }
  ]

} ]

3 个答案:

答案 0 :(得分:0)

尝试使用传播运算符更新data[i]

 for (var i = 0; i < data.length; i++) {
  data[i].properties.forEach((arrayItem, i) => {
    if (arrayItem.key === 'country') {
      data[i] = { ...data[i] , arrayItem }
    }
  });
}

答案 1 :(得分:0)

您可以使用Object.fromEntries

for (let item of data) {
  Object.assign(item,
    Object.fromEntries(item.properties.map(({key, value}) => [key, value]))
  );
}

如果您只想添加这些对中的 some 个,则可以将.filter链接到.map结果中,或者如果您只需要 one < / em>(“国家/地区”),请使用更基本的编程模式:

for (let item of data) {
  item.country = item.properties.find(({key}) => key == "country").value;
}

答案 2 :(得分:0)

这将创建一个新数组,其中包含对象的副本,如果发现,则具有附加的country属性:

const addCountry = data => data .map (({properties, ...rest}) => {
  const country = properties .find (({key}) => key == 'country')
  return {
    ... rest,
    ... (country ? {country: country .value} : {}),
    properties
  }
})


const data = [{flowId: 7079, flowName: "jackson-demo", version: 1, CreatedDate: "2020-04-02", UpdateDate: "", LastRunDate: "2020-04-02", active: false, properties: [{id: 7080, key: "country", value: "in", category: "General"}, {id: 7081, key: "source", value: "hive", category: "General"}]}, {flowId: 7079, flowName: "jackson-demo", version: 1, CreatedDate: "2020-04-02", UpdateDate: "", LastRunDate: "2020-04-02", active: false, properties: [{id: 7080, key: "country", value: "au", category: "General"}, {id: 7081, key: "source", value: "aws", category: "General"}]}];

console .log (
  addCountry (data)
)
.as-console-wrapper {min-height: 100% !important; top: 0}

如果提取已经满足了在输出中仅使用properties的要求,那么您可以跳过输出中的properties行,并使用更轻量的版本进行进一步处理。

我们当然可以选择对原始数据进行变异,但是我们不是蛮夷的,对吗?