0

In PHP I can set variable to be $arr['test1'][0]['test2']

But in JavaScript there are no associative arrays but objects. How can I make situation where I have JSON objects that are different always and I want to store fields if there exists, and store null if not like in PHP.

test1 = temp.test1[0].test

Gives me error - cannot access property test of undefined.

I know that there is workaround for this that I can check if there is property, but I have many different objects, and there are deeply nested, so I need solution where it is automatic, if whole nested property exist store, if not (or if it is property of undefined) store null.

Josh KG
  • 5,050
  • 3
  • 20
  • 24
Dejan Milosevic
  • 313
  • 4
  • 14
  • why do you need to do that? – Alnitak Apr 28 '16 at 16:19
  • Because i am listening for data that can have different properties, and i have to override them in database for that object but field names are different. For example, if score1 fields is empyy on data.scores[0].score then score1 is null in database untill we have that thata in object, and that i have for 100 types of potential properties. – Dejan Milosevic Apr 28 '16 at 16:49

0 Answers0