简体   繁体   中英

How to combine two data sets and return a single object?

I am writing my code in NodeJS and have a object which looks as below.

const data =
{
  "error1": {
    "7": [
      {
        "ErrorType": "Error-1A",
        "Hostnames": "host123.com,hostabc.com,host33a.com..."
      }
    ],
    "8": [
      {
        "ErrorType": "Error-1B",
        "Hostnames": "host223.com,host2c.com,host43a.com..."
      },
      {
        "ErrorType": "Error-1C",
        "Hostnames": "host1231.com,host2abc.com,host313a.com..."
      }
    ]
  },
  "error2": {
  ...
  },
  "error3": {
  ... 
  }
}

I have a function fetchHostDetails(hostList) which takes hostList as input and returns response as below:

[
  {
    "resType": "unknow data",
    "res": "missing data"
  },
  {
    "resType": "login failed",
    "res": "login with wrong userid"
  }
  ....
]

So far I have written the following code and it is working as expected.

    for (let key in data) {
        for (let number in data[key]) {
            data[key][number].map( async d=> {
                const fetchResponse = await sequelize
                    .query(await fetchHostDetails(d.Hostnames), options);
            });
        }
    }
    
    async function fetchHostDetails(hostList) {
    ...
    return fetchResponse;

}

I want to embed the response from the function fetchHostDetails(hostList) to data and want to make sure that is shown after Hostnames in data I have written following code but it not updating data object

    for (let key in data) {
        for (let number in data[key]) {
            data[key][number].map( async d=> {
                const fetchResponse = await sequelize
                    .query(await fetchHostDetails(d.Hostnames), options);
                data[key][number]['Fetchresult'] =  fetchResponse;
            });
        }
    }
    return data;

Desired Output:

{
  "error1": {
    "7": [
      {
        "ErrorType": "Error-1A",
        "Hostnames": "host123.com,hostabc.com,host33a.com...",
        "Fetchresult": [
      {
        "resType": "unknow data1A",
        "res": "missing data"
      },
      {
        "resType": "login failed1A",
        "res": "login with wrong userid"
      }
    ]
      }
    ],
    "8": [
      {
        "ErrorType": "Error-1B",
        "Hostnames": "host223.com,host2c.com,host43a.com...",
        "Fetchresult": [
      {
        "resType": "unknow data1B",
        "res": "no record"
      },
      {
        "resType": "login failed1B",
        "res": "wrong response"
      }
    ]
    ...
      },
      {
        "ErrorType": "Error-1C",
        "Hostnames": "host1231.com,host2abc.com,host313a.com...",
        Fetchresult": [
      {
        "resType": "error response1C",
        "res": "response details"
      },
      {
        "resType": "login failed1C",
        "res": "response details"
      }
    ]
    ...
      }
    ]
  },
  "error2": {
  ...
  },
  "error3": {
  ... 
  }
}

It's important the fact that you're using a void function in the map operator, you need to return the new object in the function:

for (let key in data) {
    for (let number in data[key]) {
        data[key][number].map( async d=> {
            const fetchResponse = await sequelize
                .query(await fetchHostDetails(d.Hostnames), options);
             return ({...d, Fetchresult : fetchResponse});
        });
    }
}
return data;

I'm using the spread operator to ensure that the keys of d are on the same level as Fetchresult

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