LoDash - an array of DeepFlatten objects

I have the following collection. Every object can have an array of objects children

, which can have an array of objects children

, etc ...

[
  {
    "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 flatten a collection recursively to get 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


source to share


3 answers


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>
      

Run codeHide result


+4


source


You can do this with a recursive function calling itself for each child and concatenating 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 one should be returned.

+1


source


You may 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);

      

0


source







All Articles