[英]get the value of the property from the array of objects. objects with children
I am trying to get from here : 我试图从这里得到:
var example=[
{
"value": 4,
"label": "Bangalore",
"path": "fa fa-tachometer",
"parentmenu": null,
"layout": null,
"children": [
{
"value": 5,
"label": "HSR Layout",
"path": "fa fa-tachometer",
"parentmenu": 4,
"layout": null,
"children": [
{
"value": 6,
"label": "Building1",
"path": "fa fa-tachometer",
"parentmenu": 5,
"layout": null,
"children": [
{
"value": 7,
"label": "Floor1",
"path": "fa fa-tachometer",
"parentmenu": 6,
"layout": null,
"children": [
{
"value": 8,
"label": "Telemon_12765213",
"path": "fa fa-tachometer",
"parentmenu": 7,
"layout": null,
"children": [
{
"value": 9,
"label": "Battery",
"path": "fa fa-tachometer",
"parentmenu": 8,
"layout": null,
"children": []
},
{
"value": 10,
"label": "MSensor",
"path": "fa fa-tachometer",
"parentmenu": 8,
"layout": null,
"children": []
}
]
},
{
"value": 11,
"label": "Telemon_12765245",
"path": "fa fa-tachometer",
"parentmenu": 7,
"layout": null,
"children": [
{
"value": 12,
"label": "Battery",
"path": "fa fa-tachometer",
"parentmenu": 11,
"layout": null,
"children": []
},
{
"value": 13,
"label": "MSensor",
"path": "fa fa-tachometer",
"parentmenu": 11,
"layout": null,
"children": []
}
]
}
]
},
{
"value": 14,
"label": "Floor2",
"path": "fa fa-tachometer",
"parentmenu": 6,
"layout": null,
"children": [
{
"value": 15,
"label": "Telemon_12765248",
"path": "fa fa-tachometer",
"parentmenu": 14,
"layout": null,
"children": [
{
"value": 16,
"label": "Battery",
"path": "fa fa-tachometer",
"parentmenu": 15,
"layout": null,
"children": []
},
{
"value": 17,
"label": "MSensor",
"path": "fa fa-tachometer",
"parentmenu": 15,
"layout": null,
"children": []
}
]
}
]
}
]
},
{
"value": 18,
"label": "Building2",
"path": "fa fa-tachometer",
"parentmenu": 5,
"layout": null,
"children": [
{
"value": 19,
"label": "Floor1",
"path": "fa fa-tachometer",
"parentmenu": 18,
"layout": null,
"children": [
{
"value": 20,
"label": "Telemon_12765254",
"path": "fa fa-tachometer",
"parentmenu": 19,
"layout": null,
"children": [
{
"value": 21,
"label": "Battery",
"path": "fa fa-tachometer",
"parentmenu": 20,
"layout": null,
"children": []
},
{
"value": 22,
"label": "MSensor",
"path": "fa fa-tachometer",
"parentmenu": 20,
"layout": null,
"children": []
}
]
}
]
}
]
}
]
}
]
}
];
to here: 到这里:
var a=[4, 5, 6, 7, 8, 11, 14, 15, 18, 19, 20];
Obviously I could just loop it and build the array but I could just get the keys from the parent but I want the values of all the objects. 显然,我可以循环并构建数组,但是我可以从父级获取键,但是我想要所有对象的值。 parents as well children.
父母和孩子。
Try with JSON.parse()
.match the value
name key then push the respected values
to the array 尝试使用
JSON.parse()
匹配value
名称键,然后将受尊重的values
推入数组
var example = [{ "value": 4, "label": "Bangalore", "path": "fa fa-tachometer", "parentmenu": null, "layout": null, "children": [{ "value": 5, "label": "HSR Layout", "path": "fa fa-tachometer", "parentmenu": 4, "layout": null, "children": [{ "value": 6, "label": "Building1", "path": "fa fa-tachometer", "parentmenu": 5, "layout": null, "children": [{ "value": 7, "label": "Floor1", "path": "fa fa-tachometer", "parentmenu": 6, "layout": null, "children": [{ "value": 8, "label": "Telemon_12765213", "path": "fa fa-tachometer", "parentmenu": 7, "layout": null, "children": [{ "value": 9, "label": "Battery", "path": "fa fa-tachometer", "parentmenu": 8, "layout": null, "children": [] }, { "value": 10, "label": "MSensor", "path": "fa fa-tachometer", "parentmenu": 8, "layout": null, "children": [] }] }, { "value": 11, "label": "Telemon_12765245", "path": "fa fa-tachometer", "parentmenu": 7, "layout": null, "children": [{ "value": 12, "label": "Battery", "path": "fa fa-tachometer", "parentmenu": 11, "layout": null, "children": [] }, { "value": 13, "label": "MSensor", "path": "fa fa-tachometer", "parentmenu": 11, "layout": null, "children": [] }] }] }, { "value": 14, "label": "Floor2", "path": "fa fa-tachometer", "parentmenu": 6, "layout": null, "children": [{ "value": 15, "label": "Telemon_12765248", "path": "fa fa-tachometer", "parentmenu": 14, "layout": null, "children": [{ "value": 16, "label": "Battery", "path": "fa fa-tachometer", "parentmenu": 15, "layout": null, "children": [] }, { "value": 17, "label": "MSensor", "path": "fa fa-tachometer", "parentmenu": 15, "layout": null, "children": [] }] }] }] }, { "value": 18, "label": "Building2", "path": "fa fa-tachometer", "parentmenu": 5, "layout": null, "children": [{ "value": 19, "label": "Floor1", "path": "fa fa-tachometer", "parentmenu": 18, "layout": null, "children": [{ "value": 20, "label": "Telemon_12765254", "path": "fa fa-tachometer", "parentmenu": 19, "layout": null, "children": [{ "value": 21, "label": "Battery", "path": "fa fa-tachometer", "parentmenu": 20, "layout": null, "children": [] }, { "value": 22, "label": "MSensor", "path": "fa fa-tachometer", "parentmenu": 20, "layout": null, "children": [] }] }] }] }] }] }]; var res =[] JSON.parse(JSON.stringify(example), function(keys,values){ if(keys == 'value') res.push(values) }) console.log(res)
you can use the recursive function to reach each node like this. 您可以使用递归函数像这样到达每个节点。
var arr = [];
function getValues(example){
example.forEach(function(obj){
//check if object has children i.e. it is a parent
if(obj.children.length){
arr.push(obj.value);
getValues(obj.children);
}
});
}
getValues(example);
console.log(arr);
You could use an iterative an recursive approach and check if the children have a length. 您可以使用迭代的递归方法,并检查子级是否有长度。
var data = [{ value: 4, label: "Bangalore", path: "fa fa-tachometer", parentmenu: null, layout: null, children: [{ value: 5, label: "HSR Layout", path: "fa fa-tachometer", parentmenu: 4, layout: null, children: [{ value: 6, label: "Building1", path: "fa fa-tachometer", parentmenu: 5, layout: null, children: [{ value: 7, label: "Floor1", path: "fa fa-tachometer", parentmenu: 6, layout: null, children: [{ value: 8, label: "Telemon_12765213", path: "fa fa-tachometer", parentmenu: 7, layout: null, children: [{ value: 9, label: "Battery", path: "fa fa-tachometer", parentmenu: 8, layout: null, children: [] }, { value: 10, label: "MSensor", path: "fa fa-tachometer", parentmenu: 8, layout: null, children: [] }] }, { value: 11, label: "Telemon_12765245", path: "fa fa-tachometer", parentmenu: 7, layout: null, children: [{ value: 12, label: "Battery", path: "fa fa-tachometer", parentmenu: 11, layout: null, children: [] }, { value: 13, label: "MSensor", path: "fa fa-tachometer", parentmenu: 11, layout: null, children: [] }] }] }, { value: 14, label: "Floor2", path: "fa fa-tachometer", parentmenu: 6, layout: null, children: [{ value: 15, label: "Telemon_12765248", path: "fa fa-tachometer", parentmenu: 14, layout: null, children: [{ value: 16, label: "Battery", path: "fa fa-tachometer", parentmenu: 15, layout: null, children: [] }, { value: 17, label: "MSensor", path: "fa fa-tachometer", parentmenu: 15, layout: null, children: [] }] }] }] }, { value: 18, label: "Building2", path: "fa fa-tachometer", parentmenu: 5, layout: null, children: [{ value: 19, label: "Floor1", path: "fa fa-tachometer", parentmenu: 18, layout: null, children: [{ value: 20, label: "Telemon_12765254", path: "fa fa-tachometer", parentmenu: 19, layout: null, children: [{ value: 21, label: "Battery", path: "fa fa-tachometer", parentmenu: 20, layout: null, children: [] }, { value: 22, label: "MSensor", path: "fa fa-tachometer", parentmenu: 20, layout: null, children: [] }] }] }] }] }] }], values = data.reduce(function iter(r, a) { return a.children && a.children.length ? r.concat(a.value, a.children.reduce(iter, [])) : r; }, []); console.log(values);
.as-console-wrapper { max-height: 100% !important; top: 0; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.