最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypescriptAngular printing out object array elements and grabbing specific elements - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited Sep 24, 2018 at 8:15 Aleksey Solovey 4,1913 gold badges17 silver badges34 bronze badges asked Sep 24, 2018 at 8:10 J KaitJ Kait 151 gold badge1 silver badge5 bronze badges 3
  • post your code please. – Lia Commented Sep 24, 2018 at 8:11
  • Beware: this might be async. Can you please show the code which is populating the projects variable? – briosheje Commented Sep 24, 2018 at 8:16
  • Unfortunately, this is a school project and deals with other students' data and so I do not wish to post my code. Sorry. – J Kait Commented Sep 24, 2018 at 18:26
Add a ment  | 

2 Answers 2

Reset to default 5

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));

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.

发布评论

评论列表(0)

  1. 暂无评论