0

I have the following JavaScript object

[
  {
    "familyName": "Smith",
    "children": [
      {         "firstName": "John"       },
      {         "firstName": "Mike"       }
    ]
  },
  {
    "familyName": "Williams",
    "children": [
      {         "firstName": "Mark"       },
      {         "firstName": "Dave"       }
    ]
  },
  {
    "familyName": "Jones",
    "children": [
      {         "firstName": "Mary"       },
      {         "firstName": "Sue"        }
    ]
  }
]

I’d like to create an array of all children i.e.

[
  {     "FirstName": "John"   },
  {     "FirstName": "Mike"   },
  {     "FirstName": "Mark"   },
  {     "FirstName": "Dave"   },
  {     "FirstName": "Mary"   },
  {     "FirstName": "Sue"    }
]

I am using jQuery.

I have looked at posts that describe merging or concatenating arrays but not those of child arrays: e.g. Merge/flatten an array of arrays in JavaScript?

I believe I could loop through the families and add the children arrays but suspect that there is a 'one-liner' for this?

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Mark Chidlow
  • 1,432
  • 2
  • 24
  • 43

4 Answers4

5

I tested in the console:

//The families(duh)
const families = [
{
    "familyName": "Smith",
    "children": [
      {         "firstName": "John"       },
      {         "firstName": "Mike"       }
    ]
  },
  {
    "familyName": "Williams",
    "children": [
      {         "firstName": "Mark"       },
      {         "firstName": "Dave"       }
    ]
  },
  {
    "familyName": "Jones",
    "children": [
      {         "firstName": "Mary"       },
      {         "firstName": "Sue"        }
    ]
  }
]
//Just flatten the children:
var children = [].concat.apply([], families.map(family => family.children));
//Outputs
console.log(children);
Lucas
  • 1,259
  • 15
  • 25
2

A solution without jQuery would be to use reduce to extract children from their families (sounds a bit rough, sorry for that).

families.reduce(function(list, family){ 
    return list.concat(family.children);
}, []);
Caramiriel
  • 7,029
  • 3
  • 30
  • 50
0

You can do this by using $.map:

var a =  [
  {
    "familyName": "Smith",
    "children": [
      {         "firstName": "John"       },
      {         "firstName": "Mike"       }
    ]
  },
  {
    "familyName": "Williams",
    "children": [
      {         "firstName": "Mark"       },
      {         "firstName": "Dave"       }
    ]
  },
  {
    "familyName": "Jones",
    "children": [
      {         "firstName": "Mary"       },
      {         "firstName": "Sue"        }
    ]
  }
]

console.log($.map(a, function(it){
    return it.children;
}));

// Or ES6
$.map(a, it => it.children);

Result:

[  
   {  
      "firstName":"John"
   },
   {  
      "firstName":"Mike"
   },
   {  
      "firstName":"Mark"
   },
   {  
      "firstName":"Dave"
   },
   {  
      "firstName":"Mary"
   },
   {  
      "firstName":"Sue"
   }
]
Niels
  • 48,601
  • 4
  • 62
  • 81
0

Try with Array#forEach method .then push the children object with new array

var families = [ { "familyName": "Smith", "children": [ { "firstName": "John" }, { "firstName": "Mike" } ] }, { "familyName": "Williams", "children": [ { "firstName": "Mark" }, { "firstName": "Dave" } ] }, { "familyName": "Jones", "children": [ { "firstName": "Mary" }, { "firstName": "Sue" } ] } ]
var children = [];
families.forEach(family => family.children.forEach(child => children.push(child)));
console.log(children);
Lucas
  • 1,259
  • 15
  • 25
prasanth
  • 22,145
  • 4
  • 29
  • 53