Deep concatenation of nested array object values using Lodash

Viewed 1869

I have an array of objects, and each of those objects may contain children, which is another array of the same structure. I want to concatenate values of each object into a list of strings delimited with a character.

For example:

var array = [{
    "text": "one",
    "children": [
      {
        "text": "two",
        "children": [
          {
            "text": "foo"
          },
          {
            "text": "bar"
          },
          {
            "text": "baz"
          }
        ]
      },
      {
        "text": "three",
        "children": [
          {
            "text": "foo"
          },
          {
            "text": "bar"
          },
          {
            "text": "baz"
          }
        ]
      }
    ]
}, {
    "text": "two",
    "children": [
      {
        "text": "hello",
        "children": [
          {
            "text": "world"
          },
          {
            "text": "planet"
          }
        ]
      }
    ]
}];

Would result in:

[
    "one two foo",
    "one two bar",
    "one two baz",
    "one three foo",
    "one three bar",
    "one three baz",
    "two hello world",
    "two hello planet"
];

Is there any way this can be achieved using Lodash?

2 Answers
Related