简体   繁体   中英

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.

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:

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:

 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 :

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:

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:

 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.

We could, of course, choose to mutate the original data, but we're not barbarians, right?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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