-4

I have some json like this

 const help = [
      {
        "en": [
          {
            "test2": [
              {
                "title": "Naslov1",
                "subtitle": "Podnaslov1",
                "answers": [
                  {
                    "answer": "Odgovor 11"
                  },
                  {
                    "answer": "Odgovor 12"
                  }
                ]
              }
            ],
            "test1": [
              {
                "title": "Naslov2",
                "subtitle": "Podnaslov2",
                "answers": [
                  {
                    "answer": "Odgovor 21"
                  },
                  {
                    "answer": "Odgovor 22"
                  }
                ]
              }
            ]
          }
        ]
      }
    ]

I need to filter this json to some property, i have properties en and test2 my new object should look like

const newArray =  [ {
                    "title": "Naslov1",
                    "subtitle": "Podnaslov1",
                    "answers": [
                      {
                        "answer": "Odgovor 11"
                      },
                      {
                        "answer": "Odgovor 12"
                      }
                    ]
                  }]

I have tried help.en.test2 but i got error TypeError: Cannot read property 'test2' of undefined

Anyone can help me how to remap this, thanks

Aluan Haddad
  • 29,886
  • 8
  • 72
  • 84
Miomir Dancevic
  • 6,726
  • 15
  • 74
  • 142
  • 1
    Duplicate of [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json). Also note that [that's not JSON](https://stackoverflow.com/a/2904181). – Guy Incognito Sep 09 '20 at 06:15
  • 2
    These are all arrays, have you tried `help[0].en[0].test2[0]`? – Jason Goemaat Sep 09 '20 at 06:15

3 Answers3

0

You need to use help[0].en[0].test2 as help and en are an array and your data is at index 0.

Philipp Meissner
  • 5,273
  • 5
  • 34
  • 59
Aakash Garg
  • 10,649
  • 2
  • 7
  • 25
0

You should try: help[0].en[0].test2

Philipp Meissner
  • 5,273
  • 5
  • 34
  • 59
Cirrus Minor
  • 394
  • 4
  • 14
0

There are a lot of questions on SO like this. If you're having trouble figuring out the structure of some JSON, it helps to use the chrome developer console. If you paste your code into the chrome dev console you can try things with test.

Just looking at it, help is an array, so help.en will be undefined, you need to use help[0].en.

Then looking at that, en is an array, so help[0].en.test2 will also be undefined. You have to do help[0].en[0].test2.

Of course that is also an array...

Jason Goemaat
  • 28,692
  • 15
  • 86
  • 113