Skip to content Skip to sidebar Skip to footer

Angular2 Typescript How To Retrieve An Array From Inside An Object

When I do console.log of the following: console.log(this.categories); I get this structure: which was created from this json from the api: [ { 'id':'dff0bb3e-889f-43e6-b955-5

Solution 1:

You want to find object that has name of 'Wood', then get category_types. Array.prototype.find returns first value that meets the condition in the provided test function. In your case, it could look like this:

this.categories.find(value => value.name === 'Wood').category_types

Solution 2:

You can use Array.filter:

let wood = this.categories.filter(category => {
  return category.name === "Wood";
})[0];

This will filter your categories to retrieve the ones with name wood, then you take the first one. If you didn't find anything with name === "Wood" in your array, then it will be undefined.

Post a Comment for "Angular2 Typescript How To Retrieve An Array From Inside An Object"