在json中查找密钥

时间:2018-11-15 09:40:39

标签: javascript node.js json recursion

我有以下json:

let object = {
"statusCode": 200,
"body": [{
        "id": "3",
        "externalId": "billgates",
        "status": "active",
        "createdAt": "2018-11-14T08:36:50.967Z",
        "updatedAt": "2018-11-14T08:36:50.967Z",
        "firstName": "yehu",
        "lastName": "da",
        "email": "bg@g.com"
    }
],
"headers": {
    "x-powered-by": "Express",
    "access-control-allow-origin": "*",
    "content-type": "application/json; charset=utf-8",
    "content-length": "189",
    "etag": "W/\"bd-Emx3/KChQLzf9+6bgFSHXPQgDTM\"",
    "date": "<<Masked>>",
    "connection": "close"
},
"request": {
    "uri": {
        "protocol": "http:",
        "slashes": true,
        "auth": null,
        "host": "mysite",
        "port": "4202",
        "hostname": "mysite",
        "hash": null,
        "search": "?username=billgates",
        "query": "username=billgates",
        "pathname": "/v1/users",
        "path": "/v1/users?username=billgates",
        "href": "http://mysite"
    },
    "method": "GET",
    "headers": {
        "Content-Type": "application/json",
        "accept": "application/json",
        "content-length": 2
    }
}

}

我编写了一个函数,该函数获取一个键和一个值,如果在json中找到的具有该值的键返回true,否则返回false,这是该函数:

let key = "externalId";
let value = "bilgates"
let findValue = function findValue(obj, key, value) {
            for(let localKey in obj){
                if(obj.hasOwnProperty(localKey)){
                    if(localKey === key){
                        res = obj[localKey] === value;
                        return res;
                    }
                    else{
                        let val = obj[localKey];
                        findValue(val, key, value);
                    }
                }
            }
        }
 let res = findValue(object, key, value)
 console.log(res);

在VS Code(node.js)中运行该函数后,出现以下错误:

RangeError:超出了最大调用堆栈大小

调试功能后,我找不到问题。似乎在某些时候之后,localKey将始终为零,而obj [localKey]将始终为3。

1 个答案:

答案 0 :(得分:0)

尽管有不同的解决方案,您仍然可以尝试以下方法

let object = {
"statusCode": 200,
"body": [{
        "id": "3",
        "externalId": "billgates",
        "status": "active",
        "createdAt": "2018-11-14T08:36:50.967Z",
        "updatedAt": "2018-11-14T08:36:50.967Z",
        "firstName": "yehu",
        "lastName": "da",
        "email": "bg@g.com"
    }
],
"headers": {
    "x-powered-by": "Express",
    "access-control-allow-origin": "*",
    "content-type": "application/json; charset=utf-8",
    "content-length": "189",
    "etag": "W/\"bd-Emx3/KChQLzf9+6bgFSHXPQgDTM\"",
    "date": "<<Masked>>",
    "connection": "close"
},
"request": {
    "uri": {
        "protocol": "http:",
        "slashes": true,
        "auth": null,
        "host": "mysite",
        "port": "4202",
        "hostname": "mysite",
        "hash": null,
        "search": "?username=billgates",
        "query": "username=billgates",
        "pathname": "/v1/users",
        "path": "/v1/users?username=billgates",
        "href": "http://mysite"
    },
    "method": "GET",
    "headers": {
        "Content-Type": "application/json",
        "accept": "application/json",
        "content-length": 2
    }
}
}

function findValue(obj, k, v) {
  let res = false

  if(!obj || typeof obj != "object") return false

  if (obj[k] == v) return true
  else {
	for (let [key, value] of Object.entries(obj)) {
          if(Array.isArray(value)) { res = res || value.some(d => findValue(d, k, v)) }
	  if(typeof value == "object") { res = res || findValue(value, k, v) }
    }
  }
  return res
}

console.log(findValue(object, 'externalId', 'billgates'))

console.log(findValue(object, 'lastName', 'da'))

console.log(findValue(object, 'x-powered-by', 'Express'))

console.log(findValue(object, 'externalId', 'Nitish'))