我有一个 Javascript 对象,如下所示。我想遍历对象并找出值Hostnames
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"
}
]
}
}
我写了以下 NodeJS 函数:
const findObjectByLabel = function(obj, label) {
if(obj.label === label) { return obj; }
for(var i in obj) {
if(obj.hasOwnProperty(i)){
var foundLabel = findObjectByLabel(obj[i], label);
if(foundLabel) { return foundLabel; }
}
}
return null;
};
const hostNames = findObjectByLabel(data, 'Hostnames');
console.log(hostNames);
上面的函数抛出错误:
Error: undefined : RangeError: Maximum call stack size exceeded
函数式编程
红颜莎娜
相关分类