Robin Glover
Robin Glover

Reputation: 1

How can I get the length of an array of objects which is within an array of objects in javascript?

I have attempted googling this question and have searched through stackoverflow but with no success so as a last resort I'm asking just in case anyone can point me in the right direction.

I have put an example of what my code would look like below.

var objectInfo = [
{
    name:"object1",
    materials: [{
        name: "wood",
        quantity:10
    }, {
        name: "stone",
        quantity:16
    }],
}, {
    name:"object2",
    materials: [{
        name: "wood",
        quantity:10
    }, {
        name: "stone",
        quantity:16
    }, {
        name: "bricks",
        quantity:100
    }],
}]

My question is, how would I get the length of my materials object array?

My attempt to achieve this was as follows:

function getMaterialsArrayLength(objName) {
    for (i = 0; i < objectInfo.length; i++) {
        if (objectInfo[i].name == objName) {
            return objectInfo[i].materials.length
        }
    }    
};

However the error returned is "cannot read property materials of undefined". Does anyone have any potential solutions to finding the array length?

I apologise if this has been posted before.

Upvotes: 0

Views: 75

Answers (4)

Ananth A
Ananth A

Reputation: 331

Using lodash.js.

var objectInfo = [
{
    name:"object1",
    materials: [{
        name: "wood",
        quantity:10
    }, {
        name: "stone",
        quantity:16
    }],
}, {
    name:"object2",
    materials: [{
        name: "wood",
        quantity:10
    }, {
        name: "stone",
        quantity:16
    }, {
        name: "bricks",
        quantity:100
    }],
}];
function getMaterialsLength(objName) {
  _.each(objectInfo, function(obj) {
      if (obj.name == objName) {  
        console.log(_.size(obj.materials));
      }
  });
}
getMaterialsLength('object2');
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.2.1/lodash.min.js"></script>

Upvotes: 0

Scott Marcus
Scott Marcus

Reputation: 65873

Just access the object by its index:

var objectInfo = [
{
    name:"object1",
    materials: [{
        name: "wood",
        quantity:10
    }, {
        name: "stone",
        quantity:16
    }],
}, {
    name:"object2",
    materials: [{
        name: "wood",
        quantity:10
    }, {
        name: "stone",
        quantity:16
    }, {
        name: "bricks",
        quantity:100
    }],
}];

console.log("objectInfo[0].materials.length: " + objectInfo[0].materials.length);
console.log("objectInfo[1].materials.length: " + objectInfo[1].materials.length);

Upvotes: 0

mpen
mpen

Reputation: 283355

var objectInfo = [{
  name: "object1",
  materials: [{
    name: "wood",
    quantity: 10
  }, {
    name: "stone",
    quantity: 16
  }],
}, {
  name: "object2",
  materials: [{
    name: "wood",
    quantity: 10
  }, {
    name: "stone",
    quantity: 16
  }, {
    name: "bricks",
    quantity: 100
  }],
}]

function getMaterialsLength(objName) {
  return objectInfo.find(o => o.name === objName).materials.length;
}

console.log(getMaterialsLength('object2'));

Might want to add some error checking.

Upvotes: 1

Scott Hunter
Scott Hunter

Reputation: 49920

You cannot get the length of an array that does not exist; more precisely, the element of the array you looking at is undefined, so has no material to find the length of:

Upvotes: 0

Related Questions