LoDash - DeepFlatten array of objects

2k views Asked by At

I have the following collection. Each object may have a children array of objects, which may have a children array of objects and so on...

[
  {
    "name": "John",
    "age": 24,
    "children": [
      {
        "name": "Jack",
        "age": 53,
        "children": [
          {
            "name": "Jenny",
            "age": 88
          }
        ]
      }
    ]
  },
  {
    "name": "George",
    "age": 45,
    "children": [
      {
        "name": "Chris",
        "age": 38,
        "children": [
          {
            "name": "Nick",
            "age": 35,
            "children": [
              {
                "name": "Maria",
                "age": 63
              }
            ]
          }
        ]
      }
    ]
  }
]

I want to recursively flatten the collection in order to have the following result:

[
  {
    "name": "John",
    "age": 24
  },
  {
    "name": "Jack",
    "age": 53
  },
  {
    "name": "Jenny",
    "age": 88
  },
  {
    "name": "George",
    "age": 45
  },
  {
    "name": "Chris",
    "age": 38
  },
  {
    "name": "Nick",
    "age": 35
  },
  {
    "name": "Maria",
    "age": 63
  }
]

How can i do this in lodash.js?

3

There are 3 answers

0
Walter Chapilliquen - wZVanG On BEST ANSWER

Try:

var data=[{"name":"John","age":24,"children":[{"name":"Jack","age":53,"children":[{"name":"Jenny","age":88}]}]},{"name":"George","age":45,"children":[{"name":"Chris","age":38,"children":[{"name":"Nick","age":35,"children":[{"name":"Maria","age":63}]}]}]}]

function getPeople(persons){
  var result = [];
  _.each(persons, function(person){
    result.push({name: person.name, age: person.age});
    person.children && (result = _.union(result,getPeople(person.children)))
  });
  return result
}

document.write(JSON.stringify(getPeople(data)))
<script src="https://lodash.com/_js/lodash.js"></script>

0
Aracthor On

You can do it with a recursive function calling itself on each children element and concatenating it in the same array:

function listPeople(list) {
    var people = [];

    for (var person in list) {
         people.push({name: person.name, age: person.age});
         if (person.children) {
              var children = listPeople(person.children);
              people = people.concat(children);
         }
    }

    return people;
}

Then just give it your first array, and the second should be returned.

2
Kiet Thanh Vo On

You can try:

var _ = require('lodash');
var a = [
    {
        "name": "John",
        "age": 24,
        "children": [
            {
                "name": "Jack",
                "age": 53,
                "children": [
                    {
                        "name": "Jenny",
                        "age": 88
                    }
                ]
            }
        ]
    },
    {
        "name": "George",
        "age": 45,
        "children": [
            {
                "name": "Chris",
                "age": 38,
                "children": [
                    {
                        "name": "Nick",
                        "age": 35,
                        "children": [
                            {
                                "name": "Maria",
                                "age": 63
                            }
                        ]
                    }
                ]
            }
        ]
    }
];

var list = [];
function filter(arr, items){
    if(_.isArray(arr) && _.isArray(items)){
        _.forEach(items, function(item){
            if(item.name && item.age) {
                arr.push({
                    name: item.name,
                    age: item.age
                });
            }
            if(item.children && _.isArray(item.children)){
                filter(arr,item.children);
            }
        });
    }
}

filter(list, a);
console.log(list);