0

How can I Access 'newsInfo' in this object with moreInfo key?

let news = {
  newsId : '1',
  newsTitle : 'Article',
  newsInfo : [
    {
      lessInfo :'new online shop'
    },
    {
      moreInfo : 'create new online shop for merchants'
    }
  ]
};
Roffulus
  • 33
  • 5
  • So you have "create new online shop for merchants" and the `news` object? If so, why not just say `news.newsInfo` or `news["newsInfo"]`? – NumberC Jun 28 '20 at 23:37
  • Task is that i have to grab moreInfo with key and console log it but i dont know how to access it – Roffulus Jun 28 '20 at 23:38
  • 1
    have you tried this ```console.log(news.newsInfo[1].moreInfo)``` ? – Desire Kaleba Jun 28 '20 at 23:40
  • This should get you the value of moreInfo: `news.newsInfo[1].moreInfo` or `news["newsInfo"][1]["moreInfo"]`. If not, can you elaborate on what you mean by "grab moreInfo with key" – NumberC Jun 28 '20 at 23:42

1 Answers1

0

You can use the dot operator and square brackets to access the second element of the array.

const info = news.newsInfo[1].moreInfo;

let news = {
  newsId : '1',
  newsTitle : 'Article',
  newsInfo : [
    {
      lessInfo :'new online shop'
    },
    {
      moreInfo : 'create new online shop for merchants'
    }
  ]
};
const info = news.newsInfo[1].moreInfo;
console.log(info);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80