Javascript在大型对象中查找嵌套键
我试图获取嵌套在此JSON对象中的Javascript在大型对象中查找嵌套键,javascript,underscore.js,Javascript,Underscore.js,我试图获取嵌套在此JSON对象中的eth0值 "addresses": { "eth0": [ "10.0.3.188" ] }, 我使用下划线.js来简化这个过程 var _ = require('underscore')._; var jsonData = { "plays": [{ "play": { "id": "d10aae34-6713-4e14-8ad5-fa2fbf6aa2b5", "na
eth0
值
"addresses": {
"eth0": [
"10.0.3.188"
]
},
我使用下划线.js来简化这个过程
var _ = require('underscore')._;
var jsonData = {
"plays": [{
"play": {
"id": "d10aae34-6713-4e14-8ad5-fa2fbf6aa2b5",
"name": "lxc"
},
"tasks": [{
"hosts": {
"lxc.server.com": {
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"cmd": "lxc copy base \"bar69\"",
"delta": "0:00:01.417533",
"end": "2017-01-10 18:01:28.692981",
"invocation": {
"module_args": {
"_raw_params": "lxc copy base \"bar69\"",
"_uses_shell": true,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"warn": true
},
"module_name": "command"
},
"rc": 0,
"start": "2017-01-10 18:01:27.275448",
"stderr": "",
"stdout": "",
"stdout_lines": [],
"warnings": []
}
},
"task": {
"id": "297bf7b7-9ee7-4517-8763-bc3b15baa6e2",
"name": "clone from base"
}
},
{
"hosts": {
"lxc.server.com": {
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"cmd": "lxc config set \"bar69\" security.privileged true",
"delta": "0:00:00.053403",
"end": "2017-01-10 18:01:32.270750",
"invocation": {
"module_args": {
"_raw_params": "lxc config set \"bar69\" security.privileged true",
"_uses_shell": true,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"warn": true
},
"module_name": "command"
},
"rc": 0,
"start": "2017-01-10 18:01:32.217347",
"stderr": "",
"stdout": "",
"stdout_lines": [],
"warnings": []
}
},
"task": {
"id": "bc63ad6f-1808-48b8-a1de-729153d2b0c5",
"name": "Promote to privileged ct"
}
},
{
"hosts": {
"lxc.server.com": {
"_ansible_no_log": false,
"_ansible_parsed": true,
"actions": [
"start"
],
"addresses": {
"eth0": [
"10.0.3.188"
]
},
"changed": true,
"invocation": {
"module_args": {
"architecture": null,
"cert_file": "/root/.config/lxc/client.crt",
"config": null,
"description": null,
"devices": null,
"ephemeral": null,
"force_stop": false,
"key_file": "/root/.config/lxc/client.key",
"name": "bar69",
"profiles": null,
"source": null,
"state": "started",
"timeout": 30,
"trust_password": null,
"url": "unix:/var/lib/lxd/unix.socket",
"wait_for_ipv4_addresses": true
},
"module_name": "lxd_container"
},
"log_verbosity": 0,
"old_state": "stopped"
}
},
"task": {
"id": "466c0da9-6cbf-4196-aea9-109218c3ed5f",
"name": "Start CT"
}
},
{
"hosts": {
"lxc.server.com": {
"_ansible_no_log": false,
"_ansible_verbose_always": true,
"changed": false,
"invocation": {
"module_args": {
"msg": [
"10.0.3.188"
]
},
"module_name": "debug"
},
"msg": [
"10.0.3.188"
]
}
},
"task": {
"id": "978c490e-59c3-41d2-818d-ab4b557ad803",
"name": ""
}
}
]
}],
"stats": {
"lxc.server.com": {
"changed": 3,
"failures": 0,
"ok": 4,
"skipped": 0,
"unreachable": 0
}
}
}
这是我到目前为止尝试过的,但没有运气
console.log(_.findKey(_.values(jsonData.tasks)));
非常感谢您的帮助您可以使用此代码循环对象属性
for (key in jsonData){
if(key == "eth0" ){
console.log(jsonData[key].toString())
}
}
您可以在没有下划线的情况下执行此操作。您可以采取的一种方法是,使用给定的属性字符串和任务,根据删除器拆分属性(您不能使用
'.
,因为您有点式属性,如'lxc.server.com'
),然后递归地检查对象,直到找到值(或没有)
注意:下面的解决方案假设您想要传递一个带分隔符的属性字符串,但是您可以很容易地直接传递一个props数组,而不是包装helper函数
尾部cail递归解决方案
var jsonData={play:{play:{id:'d10aae34-6713-4e14-8ad5-fa2fbf6aa2b5',名称:“lxc”},任务:[{hosts:{lxc.server.com:{ansible no_log:!1,ansible_解析:!0,更改:'0,cmd:'lxc copy base“bar69',delta:'0:00:01.417533',end:'2017-01-10 18:01:28.6981',调用:{barc:'param:'“,”使用shell:!0,chdir:null,creates:null,executable:null,removes:null,warn:!0},module_name:“command”},rc:0,start:“2017-01-10 18:01:27.275448”,stderr:,stdout:,stdout:,stdout行:[],警告:[]},任务:{id:“297BF7B7B7B7-9ee7-4517-8763-BC3BAA6E2”,名称:“从基克隆”},{hosts:{lxc.server.com:{{u ansible\u no\u log:!1,{u ansible\u解析:!0,更改:!0,cmd:'lxc config set“bar69”security.privileged true',delta:'0:00:00.053403',end:'2017-01-10 18:01:32.270750',调用:{module args:{u raw\u参数:'lxc config set“bar69”security.privileged true',u使用{shell:!0,chdir null,creates:null,executable:null,executable:null,removes:null,warn:'0},module name:',rc:0,start:“2017-01-10 18:01:32.217347”,stderr:,stdout:,stdout:,stdout行:[],警告:[]},任务:{id:“bc63ad6f-1808-48b8-a1de-729153d2b0c5”,名称:“升级到特权ct”},{hosts:{“lxc.server.com:{可解析的日志:!1,{可解析的日志:!0,操作:[“开始”],地址:{10.0:[“已更改”!arg0:{{architecture:null,cert_文件:“/root/.config/lxc/client.crt”,config:null,description:null,devices:null,ephemeral:null,force_stop:!1,key_文件:“/root/.config/lxc/client.key”,name:“bar69”,profiles:null,source:null,state:“started”,超时:30,信任密码:null,url:“unix:/var/lib/lxd/unix.socket”,等待地址:!0},模块名称:“lxd_容器”}1.任务:{id:“466c0da9-6cbf-4196-aea9-AAA9-4196-AAAA9-4196-AAA9-109218C3555F,姓名:“开始CT”名称:“开始CT”0,姓名:“开始CT”0,旧的州:0,旧的州:0,旧的州州:0,旧的州州州州州:0,旧的州州州:“0,旧的州州州州州州州州州州::::::::::::,0,旧州州州:0,旧州州州州州州州州州:0,旧州州州州州州州州州州州州州州州州州:“停止:::::::::::::::::::::,0,旧州州:“停止::::::::::::::::,旧州州州:::::::::::B557AD803”,名称:“}}]}],状态:{“lxc.server.com:{changed:3,failures:0,ok:4,skipped:0,unreachable:0}};
函数getNestedPropHelper(obj,[first,…rest]){
//基本情况
if(typeof obj!=“object”| |!obj)返回未定义;
如果只有一个属性,则返回rest.length==0//
?obj[first]//返回值
:getNestedPropHelper(obj[first],rest);//否则递归返回rest
}
函数getNestedProp(obj,prop,delim='|'){
返回getNestedPropHelper(对象,属性拆分(delim));
}
//提取任务
const tasks=jsonData.plays.reduce((arr,play)=>arr.concat(play.tasks),[]);
//获取每个任务的eth0属性
const props=tasks.map(task=>
getNestedProp(任务“hosts | lxc.server.com | addresses | eth0”)
);
//记录每个任务的eth0属性(只有第三个任务实际具有该值)
console.log(props);
如果要直接访问它,请尝试以下操作:
jsonData.plays[0]。任务[2]。主机[“lxc.server.com”]。地址。eth0[0]
编辑:这是测试过的。包含任务[3]的帖子不正确。您可以使用一些功能,例如获取key host上的对象,查看每个host对象是否有密钥地址,获取值的映射,查看映射是否返回值,等等:
var hosts = _.pluck(jsonData.plays[0].tasks, 'hosts');
var mapping = _.map(hosts, function(host) {
var keys = _.keys(host);
if (_.size(keys)) {
var nestedHost = host[_.first(keys)];
if (_.property('addresses')(nestedHost)) {
if (_.property('eth0')(nestedHost.addresses)) {
return nestedHost.addresses.eth0[0];
}
}
}
});
console.log(_.filter(mapping));
请参见中的操作,以及下面的示例:
var jsonData={
“戏剧”:[{
“玩”:{
“id”:“d10aae34-6713-4e14-8ad5-fa2fbf6aa2b5”,
“名称”:“lxc”
},
“任务”:[{
“主持人”:{
“lxc.server.com”:{
“\u ansible\u no\u log”:false,
“_ansible_parsed”:正确,
“改变”:对,
“cmd”:“lxc复制基\”bar69 \“,
“增量”:“0:00:01.417533”,
“结束”:“2017-01-10 18:01:28.692981”,
“调用”:{
“模块参数”:{
“_raw_params”:“lxc copy base\“bar69\”,
“_使用_外壳”:正确,
“chdir”:空,
“创建”:null,
“可执行文件”:null,
“删除”:null,
“警告”:正确
},
“模块名称”:“命令”
},
“rc”:0,
“开始”:“2017-01-1018:01:27.275448”,
“标准”:“,
“stdout”:“,
“标准输出线”:[],
“警告”:[]
}
},
“任务”:{
“id”:“297bf7b7-9ee7-4517-8763-bc3b15baa6e2”,
“名称”:“从基础克隆”
}
}, {
“主持人”:{
“lxc.server.com”:{
“\u ansible\u no\u log”:false,
“_ansible_parsed”:正确,
“改变”:对,
“cmd”:“lxc配置集\”bar69 \“security.privileged true”,
“增量”:“0:00:00.053403”,
“结束”:“2017-01-10 18:01:32.270750”,
“调用”:{
“模块参数”:{
“_raw_params”:“lxc配置集\”bar69 \“security.privileged true”,
“_使用_外壳”:正确,
“chdir”:空,
“创建”:null,
“可执行文件”:null,
“删除”:null,
“警告”:正确
},
“模块名称”:“c