简体   繁体   English

从对象数组获取属性的值。 带孩子的物体

[英]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.

相关问题 对象数组的对象。 获取具有相同属性的所有值的长度 - Object of array of objects. Get the length of all values with the same property 从对象数组获取对象属性值 - Get object property value from array of objects 从带有子项的对象数组中获取属性的值 - Get value of property from an array of objects with childs 在包含对象数组中的子对象的对象数组中查找值 - Find the value inside the array of objects that include children from the array of objects 比较2个对象数组。 按 id 匹配,将对象属性推入数组 - compare 2 array of objects. match by ids, push object property into array 从对象数组中获取 children 标签 - Get the children tag from an array of objects Javascript对象。 将对象实例化为对象的属性值 - Javascript Objects. Instantiation of an object as the property value of an object LoDash从子数组中找到其属性与对象数组中的值匹配的对象 - LoDash find the object whose property matches the value from array of objects with children array 如何从对象数组中获得一个从未出现在特定属性中的值 - How to get a value that never appear into specific property from the Array of objects 如何从 JavaScript 中的对象数组中获取属性值 - How to get a property value from an array of objects in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM