繁体   English   中英

如何组合两个数据集并返回单个对象?

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

我正在用 NodeJS 编写代码,并且有一个如下所示的对象。

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": {
  ... 
  }
}

我有一个函数fetchHostDetails(hostList)它将hostList作为输入并返回如下响应:

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

到目前为止,我已经编写了以下代码并且它按预期工作。

    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;

}

我想将来自函数fetchHostDetails(hostList)的响应嵌入到data并希望确保在我编写以下代码的data Hostnames之后显示,但它不更新data对象

    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;

期望输出:

{
  "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": {
  ... 
  }
}

在 map 运算符中使用 void 函数这一事实很重要,您需要在函数中返回新对象:

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;

我正在使用扩展运算符来确保 d 的键与 Fetchresult 处于同一级别

暂无
暂无

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

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