Tell me how to organize the code correctly in order to get the property of an object from an object that is in an array in a construction (if) near a mark (LITTER !!) everything seems simple but doesn’t fry =) in that construction I need to make further comparison if there is such an object the color is green and if so, then I do something further, for example it should look like this - if (myBasketKey [i] [hash] .color == 'green'), but if I write if (myBasketKey [i] [hash]. color == 'green') - there will be an error: Uncaught TypeError: Cannot read property 'color' of undefined. Below I give an example to which level I managed to get to
var myBasketKey = [ { "hash1": { "color": "BLUE", "size": "1", "count": 1 } }, { "hash2": { "color": "RED", "size": "22", "count": 22 } }, { "hash3": { "color": "GREEN", "size": "33", "count": 33 } }, { "hash4": { "color": "RED", "size": "22", "count": 22 } } ] var hash = "hash2"; switch (hash) { case 'hash1': case 'hash2': case 'hash3': for (var i = 0; i < myBasketKey.length; i++) { if (myBasketKey[i][hash]) { //ПОМЕТКА!!!!!!!!! console.log('есть такой: ', myBasketKey[i][hash]); break; } } break; default: for (var i = 0; i < myBasketKey.length; i++) { if (!myBasketKey[i].hasOwnProperty(hash)) { console.log('любое действие когда нету'); break; } } break; }