简体   繁体   English

如何将键值对添加到 object

[英]How to add key value pair to object

I have a Datasource which contains a nested array of objects.我有一个包含嵌套对象数组的数据源。 I have been able to select key value pair, and now I want to add those values to the top level of the object, ie outside the nested object.我已经能够 select 键值对,现在我想将这些值添加到 object 的顶层,即嵌套的 object 之外。

Initial array:初始数组:

 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"
  }
  ]

} ]

Using the below code I am able to get the key value pair:使用下面的代码,我可以获得键值对:

 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);
    }
  });
}

Output of Code: Output 代码:

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

How do I push these values back in the array so that my new array looks like this:如何将这些值推回数组中,以便我的新数组如下所示:

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"
  }
  ]

} ]

Try update data[i] using spread operator:尝试使用扩展运算符更新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 }
    }
  });
}

You could use Object.fromEntries :您可以使用Object.fromEntries

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

In case you only want to add some of these pairs, you can chain a .filter to the .map result, or if you just need one ("country"), use a more basic programming pattern:如果您只想添加其中一些对,您可以将.filter.map结果,或者如果您只需要一个(“国家”),请使用更基本的编程模式:

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

This creates a new array with copies of your objects having an additional country property if it's found:这将创建一个新数组,其中包含具有附加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}

If the only use of properties in the output is already satisfied by this extraction, then you could skip the properties line in the output and have more lightweight versions for further processing.如果此提取已经满足了 output 中properties的唯一使用,那么您可以跳过 output 中的properties行并拥有更多轻量级版本以进行进一步处理。

We could, of course, choose to mutate the original data, but we're not barbarians, right?当然,我们可以选择改变原始数据,但我们不是野蛮人,对吧?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM