javascript - Check values of nested items of object -
i trying check week object's minutes , hours , cannot figure out doing wrong. week object can contain variations of day1 - day7 dont want check them specifically. want check nested hours/minutes. don't want use jquery , has work ie8. appreciated.
week = { day1: { hours: 6, minutes: 20 }, day2: { minutes: 45 }, day3: { hours: 8, minutes: 15 } }; hoursinvalid = false; minutesinvalid = false; (var item in week) { if (week.hasownproperty(item)) { (var = 0; < week[item].length; i++ ) { if (week[item][i].hours > 6) { hoursinvalid = true; break; } if (week[item][i].minutes > 20) { minutesinvalid = true; break; } } } }
i don't see need internal for
loop. solution came with. added checks make sure dayn
objects have hours
, minutes
properties.
week = { day1: { hours: 6, minutes: 20 }, day2: { minutes: 45 }, day3: { hours: 8, minutes: 15 } }; hoursinvalid = false; minutesinvalid = false; (var item in week) { if (week[item].hasownproperty('hours')) { if (week[item].hours > 6) { hoursinvalid = true; break; } } if (week[item].hasownproperty('minutes')) { if (week[item].minutes > 20) { minutesinvalid = true; break; } } }
Comments
Post a Comment