javascript - Flat mapping and projecting an array with Underscore -


How do I align the parental objects in an array that is a product of each parent and child object underscore with? Take the following values:

  var parent = [[ID: 1, name: 'John', children: [[ID: 1, name: 'Nancy'}, {ID: 2, Name: 'Bob'}}} {id: 2, name: 'jack'}, {id: 3, name: 'jen', children: [id: 1, name: 'chloe'}, {id: 2 , Name: 'Lisa'}]}]   

I need the following output:

  var aggregate = [{id: 1, name: (Id: 2: name: 'Bob'}}, {id: 2, name: (id: 1, name: 'nancy'}}, {id: 1, name: 'john', child: {ID: 2, name: 'jack', child: null}, {id: 1, name: 'jen', child: {ID: 1, name: 'chloe'}}, {id: 1, no : 'Jane', child: {ID: 2, name: 'LISA'}}]   

I'm guessing that I map and Chening needs to be used.

You can map your child in an array of children and then adjust it to get a list of parents: Child

  var all = _ (parent). Chain () Map (function (parent) {// if there is no hair case ((parent. Children) || (! Parent, child. Length)) back _. Expansion (_. (Parents, 'Children'), {Children: Null}); Parents / Children's return _.map (parent.children, function (child) {return _.extend (_.) (Parent, 'children'), {child: child}}}};}). Flat (). Values ​​();   

and a demo

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -