Justin808
Justin808

Reputation: 21522

How to get the max array size from nested arrays?

I've got the following example object. There could be any depth of of data, not just the example of 3.

{
  'name': 'Clifford Shanks',
  'born': 1862,
  'died': 1906,
  'location': 'Petersburg, VA',
  'children': [
    {
      'name': 'Elizabeth Shanks',
      'born': 1795,
      'died': 1871,
      'location': 'Ireland/Petersburg, VA'
    }
  ],
  'parents': [
    {
      'name': 'James Shanks',
      'born': 1831,
      'died': 1884,
      'location': 'Petersburg, VA',
      'parents': [
        {
          'name': 'Robert Shanks',
          'born': 1781,
          'died': 1871,
          'location': 'Ireland/Petersburg, VA'
        },
        {
          'name': 'Elizabeth Shanks',
          'born': 1795,
          'died': 1871,
          'location': 'Ireland/Petersburg, VA'
        },
        {
          'name': 'Elizabeth Shanks',
          'born': 1795,
          'died': 1871,
          'location': 'Ireland/Petersburg, VA'
        }
      ]
    },
    {
      'name': 'Ann Emily Brown',
      'born': 1826,
      'died': 1866,
      'location': 'Brunswick/Petersburg, VA',
      'parents': [
        {
          'name': 'Henry Brown',
          'born': 1792,
          'died': 1845,
          'location': 'Montgomery, NC'
        },
        {
          'name': 'Sarah Houchins',
          'born': 1793,
          'died': 1882,
          'location': 'Montgomery, NC'
        }
      ]
    }
  ]
}

I want something to give me the answer 3 as one of the parents arrays has a length of 3. I though I would use lodash to pluck out all the arrays and the I'd be able to figure things out form there but when I _.pluck(data, 'parents') I get

[undefined, undefined, undefined, undefined, undefined, undefined]

which is odd more than anything because there are only 3 parents arrays but I get 6 undefineds back.

Any idea how to get the maximum parents array size?

Upvotes: 1

Views: 919

Answers (2)

al .js
al .js

Reputation: 206

If you have an unspecified depth of data, recursion is a good way to traverse the data. I don't think lodash's pluck will do a deep dive into a tree-like structure like your example.

var maxParents = 0;
var findMaxParents = function(data) {

  // if there are grandparents, let's see how many there are
  if(data.parents && data.parents.length > 0) {
    data.parents.forEach(function(parent){
      findMaxParents(parent);
    });
  }

  if(data.parents && data.parents.length > maxParents) {
    maxParents = data.parents.length;
  }
  return maxParents;
}

And here's a JSFiddle with some console.logging as well: http://jsfiddle.net/uerwzm0L/

Upvotes: 1

Brant
Brant

Reputation: 1788

Assuming your entire object above is assigned to var obj:

var size = 0;

obj.parents.forEach(function(parent) {
    if(parent.parents && parent.parents.length > size)
        size = parent.parents.length
})

// If you need to include the 'parent' parents array size, 
// add the following check
if(obj.parents.length > size)
     size = obj.parents.length;
// end check

return size;

Upvotes: 0

Related Questions