There is json.

wt = { "days": [{ "2": [{"1":"1","2":"2","4":"4","6":"6"}], "3": [{"1":"1","3":"3","7":"7"}] }], "time_from_h": [{ "2": [{"1":"8","2":"9","4":"8","6":"10"}], "3": [{"1":"8","3":"9","7":"8"}] }], "time_from_m": [{ "2": [{"1":"0","2":"5","4":"30","6":"50"}], "3": [{"1":"10","3":"0","7":"10"}] }], "time_to_h": [{ "2": [{"1":"21","2":"20","4":"22","6":"20"}], "3": [{"1":"19","3":"17","7":"19"}] }], "time_to_m": [{ "2": [{"1":"30","2":"40","4":"40","6":"30"}], "3": [{"1":"59","3":"10","7":"40"}] }] } 

How to apply to: wt.days.2.4 (get exactly the value of the item)

How to check if wt.days.2

Those. there will be a function to which id will be passed. So you need to check if in id = 2 in wt.days and if there is, then output wt.days.2.4

  • 2
    It is not clear why you have objects there wrapped in arrays from one element, but somehow like this: var i = 2; console.log(wt.days[0][i] ? wt.days[0][i][0][4] : undefined); var i = 2; console.log(wt.days[0][i] ? wt.days[0][i][0][4] : undefined); - Yaant
  • 2
    Possible duplicate question: Help to parse the code to the end - Grundy
  • @Grundy, the link to the closed question leads something. - Visman
  • @Visman, this does not make it no duplicate - Grundy 2:49 PM

1 answer 1

 var wt = { "days": [{ "2": [{"1":"1","2":"2","4":"4","6":"6"}], "3": [{"1":"1","3":"3","7":"7"}] }], "time_from_h": [{ "2": [{"1":"8","2":"9","4":"8","6":"10"}], "3": [{"1":"8","3":"9","7":"8"}] }], "time_from_m": [{ "2": [{"1":"0","2":"5","4":"30","6":"50"}], "3": [{"1":"10","3":"0","7":"10"}] }], "time_to_h": [{ "2": [{"1":"21","2":"20","4":"22","6":"20"}], "3": [{"1":"19","3":"17","7":"19"}] }], "time_to_m": [{ "2": [{"1":"30","2":"40","4":"40","6":"30"}], "3": [{"1":"59","3":"10","7":"40"}] }] }; console.log(wt.days[0][2][0][4]); // странная запись из-за того, что объекты содержат массивы, а массивы содержат объекты, отсюда и нулевые индексы console.log(typeof wt.days[0][2]); // элемент 2 есть console.log(typeof wt.days[0][4]); // элемента 4 нет, "undefined" 

  • Thank. And how to properly write such a structure? - Pavel Zhukovsky
  • one
    Which one? If you are about wt , then you need to exclude arrays (square brackets) from it and leave only objects (curly brackets), then unnecessary indices (zero) when referring to the necessary elements of the structure will not be needed. - Visman