0

I am in React.js consuming a JSON API. I am trying to loop through and get values from an array that is nested within an array.

When I do typeof on this.state.products[0] I get "Object" However, here is what it looks when I console.log it and it's parent.

{
  "products": [
    [
      1,
      1,
      "2019-09-06",
      "Tackle Amazon Machine Image",
      "ami",
      1000
    ],
    [
      2,
      1,
      "2019-09-06",
      "Tackle for GovCloud",
      "saas",
      5000
    ]
  ]
}

How can I print only "Tackle Amazon Machine Image" ? THANK YOU!

I have tried

{this.state.products[0]["1"]}
{this.state.products[0][1]}
{this.state.products[0].[1]}
{this.state.products[0].["1"]}
{this.state.products[0]."1"}
n-dev-101
  • 13
  • 4

1 Answers1

-1

var data = {
  "products": [
    [
      1,
      1,
      "2019-09-06",
      "Tackle Amazon Machine Image",
      "ami",
      1000
    ],
    [
      2,
      1,
      "2019-09-06",
      "Tackle for GovCloud",
      "saas",
      5000
    ]
  ]
};

console.log(data.products[0][3]);
tom
  • 9,550
  • 6
  • 30
  • 49
  • @CertainPerformance if I try your suggestion I get "TypeError: Cannot read property '3' of undefined" – n-dev-101 Sep 06 '19 at 22:37
  • I see "Tackle Amazon Machine Image". Do you test it in this snippet above? – tom Sep 06 '19 at 22:39
  • Yes. in the snipet it works, however in my react app it is throwing the TypeError. What is more is that when I do "typeof this.state.products[0]" I get object. However, when I try Object.keys I get "TypeError: Cannot convert undefined or null to object" – n-dev-101 Sep 06 '19 at 22:45
  • @n-dev-101 `typeof null === 'object'` – Dan Sep 06 '19 at 23:26