I have arrays of deeply nested objects. I would like to write a function to extract arbitrary child objects from these arrays. In some cases values of nested properties are values and objects, in other cases they are arrays.
Examples of arrays are below:
[{parent: {level1: {level2: 'data'}}}]
[{parent: {level1: [{level2: {...}}, {level2: {...}}, {level2: {...}}]}}]
[{parent: {level1: [{level2: {level3: 'data'}}, {level2: {..}}, {level2: {..}}]}}]
Calling extraction function on such an array should result in an array of objects that we're interested in.
Examples of calling the function and its results for the example arrays above:
extractChildren(source, 'level2') = [{level2: 'data'}]
extractChildren(source, 'level2') = [{level2: {...}, level2: {...}, level2: {...}]
extractChildren(source, 'level3') = [{level3: 'data'}]
Is there a concise way to achieve this with lodash
or I should use regular JavaScript to iterate through properties?
P.S. Think of it as equivalent of XPath select all nodes with the name "nodename"