【问题标题】:How to combine two data sets and return a single object?如何组合两个数据集并返回一个对象?
【发布时间】:2021-02-16 16:40:27
【问题描述】:

我正在用 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": {
  ... 
  }
}

【问题讨论】:

    标签: javascript node.js arrays json collections


    【解决方案1】:

    重要的是,您在 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 处于同一级别

    【讨论】:

    • data 仍然没有显示新的键值对Fetchresult。它只有原始值是因为async await 用于fetchResponse
    • 你是对的,在这种情况下,我会为所有主机创建一个单独的请求,然后合并结果
    • 您能相应地更新您的答案吗?谢谢!
    猜你喜欢
    • 2021-02-17
    • 1970-01-01
    • 1970-01-01
    • 2019-10-08
    • 1970-01-01
    • 1970-01-01
    • 2019-12-31
    • 2022-01-04
    相关资源
    最近更新 更多