gpt4 book ai didi

javascript - 在对象中查找键值

转载 作者:行者123 更新时间:2023-11-30 20:00:43 25 4
gpt4 key购买 nike

我有以下对象(Json):

 let object = {
"statusCode": 200,
"body": [{
"id": "3",
"externalId": "yehudakala4",
"status": "active",
"createdAt": "2018-11-14T08:36:50.967Z",
"updatedAt": "2018-11-14T08:36:50.967Z",
"firstName": "yehu",
"lastName": "da",
"email": "ye@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": "user-management-service.dev.local:4202",
"port": "4202",
"hostname": "user-management-service.dev.local",
"hash": null,
"search": "?username=yehudakala4",
"query": "username=yehudakala4",
"pathname": "/v1/users",
"path": "/v1/users?username=yehudakala4",
"href": "http://user-management-service.dev.local:4202/v1/users?username=yehudakala4"
},
"method": "GET",
"headers": {
"Content-Type": "application/json",
"accept": "application/json",
"content-length": 2
}
}

给定以下函数:

let key = "protocol";
let value = "http:";
let x;
let res = false;
let findValue = function findValue(obj, key, value) {

for(let localKey in obj){
if(obj.hasOwnProperty(localKey)){
//console.log(localKey)
if(localKey === key){
res = obj[localKey] === value;
return res;
}
else
{
let val = obj[localKey];
if(typeof val === 'object')
x = findValue(val, key, value);
if (typeof x === 'boolean') {
return x;
}
}
}
}
}

let rs = findValue(object, key, value)
console.log(rs);

这种方法有两个问题:

  1. 如果key在对象中不存在,则函数返回undefine
  2. 如果key出现多次,只检查第一次

我的目标是检查任何出现的键,如果其中一个键的值不匹配返回 false,如果所有匹配返回 true。

其次,如果key不存在则返回false。

最佳答案

我更改了您的函数,通过将 Set 作为递归中的上下文来获取给定键的所有值。

然后您检查集合是否有一个等于您的值的值

查看更新的代码段。

最后,作为旁注,Object.keys 将使您的代码更精简,因为您不必检查 hasOwnProperty

let findValues = function(obj, key, found) {
for (let localKey in obj) {
if (obj.hasOwnProperty(localKey)) {
let val = obj[localKey];
//console.log(localKey)
if (localKey === key) {
found.add(val)
} else {
if (typeof val === 'object') {
findValues(val, key, found)
}
}
}
}
}

function uniqueValue(obj, key, value) {
let found = new Set()
findValues(object, key, found)
return found.size === 1 && found.has(value);
}

let object = {
"statusCode": 200,
"body": [{
"id": "3",
"externalId": "yehudakala4",
"status": "active",
"createdAt": "2018-11-14T08:36:50.967Z",
"updatedAt": "2018-11-14T08:36:50.967Z",
"firstName": "yehu",
"lastName": "da",
"email": "ye@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": "user-management-service.dev.local:4202",
"port": "4202",
"hostname": "user-management-service.dev.local",
"hash": null,
"search": "?username=yehudakala4",
"query": "username=yehudakala4",
"pathname": "/v1/users",
"path": "/v1/users?username=yehudakala4",
"href": "http://user-management-service.dev.local:4202/v1/users?username=yehudakala4"
},
"method": "GET",
"headers": {
"Content-Type": "application/json",
"accept": "application/json",
"content-length": 2
}
}
}

let key = "protocol";
let value = "http:";

console.log(uniqueValue(object, key, value));

关于javascript - 在对象中查找键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53406825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com