J Kait
J Kait

Reputation: 15

Typescript/Angular printing out object array elements and grabbing specific elements

The Array

This is what appears when I do

console.log(projects)

However, whenever I try to print anything beyond that, I receive errors or it simply does not print anything.

The things I have tried:

console.log(projects[0]);
console.log(projects.title);

I have also tried using the for each element loop to no luck.

How would I grab the title of each element within this?

Upvotes: 1

Views: 13468

Answers (2)

Léo R.
Léo R.

Reputation: 2698

Try this :

console.log(projects[0].title);

First you need to access first array element and after the property

and for the foreach :

projects.forEach(projet=>console.log(projet.title));

Upvotes: 5

gr4viton
gr4viton

Reputation: 1514

Loop over your array with forEach and log the title of each objects.

const myArray = [{title: 'title1'}, {title: 'title2'}, {title: 'title3'}];

myArray.forEach(object => console.log(object.title));

You can find the documentation of forEach here.

Upvotes: 1

Related Questions