Javascript 如何在NodeJS的对象中嵌入来自方法的响应?
我有一个物体,看起来如下。 对象将始终具有Javascript 如何在NodeJS的对象中嵌入来自方法的响应?,javascript,node.js,arrays,dictionary,collections,Javascript,Node.js,Arrays,Dictionary,Collections,我有一个物体,看起来如下。 对象将始终具有3种错误类型-error1,error2,error3 const data = { "error1": { "7": [ { "ErrorType": "Error-1A", "Hostnames": "host123.com,hostabc.com,host33a.com..."
3种错误类型
-error1
,error2
,error3
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": {
"3": [
{
"ErrorType": "Error-2A"
"Hostnames": "host1231.com,host2abc.com,host313a.com..."
},
{
"ErrorType": "Error-2B"
"Hostnames": "host1231.com,host2abc.com,host313a.com..."
}
],
"8": [
{
"ErrorType": "Error-2C"
"Hostnames": "host1231.com,host2abc.com,host313a.com..."
},
{
"ErrorType": "Error-2D",
"Hostnames": "host1231.com,host2abc.com,host313a.com..."
}
]
},
"error3": {
"1": [
{
"ErrorType": "Error-3A",
"Hostnames": "host1236.com"
},
{
"ErrorType": "Error-3B",
"Hostnames": "hostc3231.com"
}
]
}
}
我有一个函数fetchHostDetails(hostnames)
,它接受hostnames
作为输入,并返回如下响应:
[
{
"resType": "unknow data",
"res": "missing data"
},
{
"resType": "login failed",
"res": "login with wrong userid"
}
....
]
上述响应将始终具有resType
和res
的值,但可以有任意数量的此类条目
对于数据
对象中的每一组主机名
,我想调用获取主机详细信息(主机名)
,并想用原始数据
对象中的响应值替换主机名
期望的响应:
{
"error1": {
"7": [
{
"ErrorType": "Error-1A",
"result": [
{
"resType": "unknow data",
"res": "missing data"
},
{
"resType": "login failed",
"res": "login with wrong userid"
}
]
}
],
"8": [
{
"ErrorType": "Error-1B",
"result": [
{
"resType": "new data",
"res": "data"
},
{
"resType": "data failed",
"res": "invalid"
}, {
"resType": "res failed",
"res": "discard"
}
]
},
{
"ErrorType": "Error-1C",
"result": [
{
"resType": "sample data",
"res": "sample"
}
]
}
]
},
"error2": {
"3": [
{
"ErrorType": "Error-2A",
"result": [
{
"resType": "Mydata",
"res": "Mydata is empty"
}
]
},
...
},
"error3": {
"1": [
...
]
}
}
到目前为止,我已经编写了以下代码
const hostNames = [];
for (let key in data) {
for (let number in data[key]) {
data[key][number].map( d=> {
hostNames.push(d.Hostnames);
});
}
}
`hostNames` array looks as below:
[
"host123.com,hostabc.com,host33a.com...",
"host223.com,host2c.com,host43a.com...",
"host1231.com,host2abc.com,host313a.com...",
"host1231.com,host2abc.com,host313a.com...",
...
]
现在,我需要调用等待fetchHostDetails(hostnames)
获取主机名中的每个值。
我还需要将响应嵌入到数据对象中。首先需要将每个主机名从该数组中的每个字符串中拆分出来。一旦这样做,您就可以将它们全部放在一个数组中(flat()
),然后循环调用fetchHostDetails(hostname)
请阅读代码中的注释。这段代码将在控制台中输出每个URL,就好像它已被传递给fetch函数一样
const数据={
“错误1”:{
"7": [{
“错误类型”:“错误-1A”,
“主机名”:“host123.com、hostabc.com、host33a.com…”
}],
"8": [{
“错误类型”:“错误-1B”,
“主机名”:“host223.com、host2c.com、host43a.com…”
},
{
“错误类型”:“错误-1C”,
“主机名”:“host1231.com、host2abc.com、host313a.com…”
}
]
},
“错误2”:{
"3": [{
“ErrorType”:“Error-2A”,
“主机名”:“host1231.com、host2abc.com、host313a.com…”
},
{
“错误类型”:“错误-2B”,
“主机名”:“host1231.com、host2abc.com、host313a.com…”
}
],
"8": [{
“错误类型”:“错误-2C”,
“主机名”:“host1231.com、host2abc.com、host313a.com…”
},
{
“ErrorType”:“Error-2D”,
“主机名”:“host1231.com、host2abc.com、host313a.com…”
}
]
},
“错误3”:{
"1": [{
“ErrorType”:“Error-3A”,
“主机名”:“host1236.com”
},
{
“ErrorType”:“Error-3B”,
“主机名”:“hostc323.com”
}
]
}
};
常数输出=[];
for(让我们输入数据){
for(数据[键]中的let编号){
数据[key][number].map(d=>out.push(d.Hostnames));
}
}
out.flat().map(异步URL列表=>{
const responseArr=wait fetchStuff(urlList);
console.log(responseArr);
});
异步函数fetchStuff(urlList){
返回URL列表;
}
我正在做responseArr.push(响应)
但是responseArr
有一个空值。我不需要分割每个主机名,因为我正在我的fetchStuff
函数中处理它。你能告诉我为什么responseArr
是空的吗?我编辑了代码/答案。看看这是否回答了你的问题。好的,让我检查一下。还有一件事,在console.log(responseArr)之后
我基本上需要将responseArr
嵌入到data
对象中,如所需输出所示。谢谢你的帮助!