I have experience only really with JS arrays, not objects, so I'm trying to learn how to navigate them. I would appreciate any answers with good explanation that show the process of completing the following task:
I have a month (expressed as an integer, 1-11
), and a collection of data for various IDs, (expressed as an integer too, e.g 474
)
The data looks like this (a subset of some longer data):
var myData = {
1: {474: true, 459: true},
4: {474: true, 578: true, 987: true, 459: true, 917: true, 296: true},
5: {474: true, 578: true, 1036: true, 391: true, 987: true, 459: true, 917: true, 296: true}
};
I need to
a) Locate the first key in my index based on my month
value. E.g var month = 4
;
b) Locate whether true or false for my ID
value in that sub-object e.g var ID = 917
Some examples of what I need to check:
if month == 1
and ID == 459
return true
if month == 1
and ID == 917
return false
(917
not present in data for month 1
if month == 4
and ID == 987
return true
if month == 5
and ID == 917
return true
if month == 5
and ID == 100
return false
(100
not present in data for month 5
)
I have safeguarded that all months 1-11
are present in the data, so I don't need to have an extra check to see if the month
value exists in the object. It just needs to locate the first key using month
and then search in that sub-object for the ID
and if it finds it return true
.