Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get list of all child elements with field from parent

Tags:

jmespath

I have list of dicts that contains another list in one of fields. I want to "flatten" that list, so it gives me every subelement with one field (or some fields) from parent copied into it. Example:

Source data:

[
    {
        "name": "A",
        "foo": "x",
        "bar": 1,
        "subelements": [
            {
                "baz": "xyz",
                "foobar": "abc"
            },
            {
                "baz": "zzz",
                "foobar": "def"
            }
        ]
    },
    {
        "name": "B",
        "foo": "Y",
        "bar": 4,
        "subelements": [
            {
                "baz": "yyy",
                "foobar": "aaa"
            },
            {
                "baz": "xxx",
                "foobar": "bbb"
            },
            {
                "baz": "www",
                "foobar": "bbb"
            }
        ]
    }
]

Expected result:

[
    {
        "baz": "xyz",
        "foobar": "abc",
        "foo": "x"
    },
    {
        "baz": "zzz",
        "foobar": "def",
        "foo": "x"
    },
    {
        "baz": "yyy",
        "foobar": "aaa",
        "foo": "Y"
    },
    {
        "baz": "xxx",
        "foobar": "bbb",
        "foo": "Y"
    },
    {
        "baz": "www",
        "foobar": "bbb",
        "foo": "Y"
    }
]
like image 767
GwynBleidD Avatar asked Oct 06 '18 20:10

GwynBleidD


People also ask

How do I get all Div children?

If You want to get list only children elements with id or class, avoiding elements without id/class, You can use document. getElementById('container'). querySelectorAll('[id],[class]'); ... querySelectorAll('[id],[class]') will "grab" only elements with id and/or class.

How do you get all the child elements in a protractor?

Find child elements (protractor chaining locators)First we need to find the parent element then using the parent element we find the child element and perform operation on it. var parent = element(by.id('get-input')); var child = parent. element(by. className('form-group')); child.


1 Answers

This is not currently possible to do without parent node reference. Parent node access is still listed as a feature request

like image 56
TRiNE Avatar answered Oct 19 '22 16:10

TRiNE