简体   繁体   English

LoDash - DeepFlatten对象数组

[英]LoDash - DeepFlatten array of objects

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... 每个对象可能有一个children对象数组,可能有一个children对象数组,依此类推......

[
  {
    "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? 我怎么能在lodash.js?这样做lodash.js?

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> 

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. 然后给它你的第一个数组,第二个应该返回。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM