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; } 

  • one
    What's the problem with the code in question? - Grundy
  • @Grundy - "but does not fool" - Igor
  • @Igor, so it seems to be doing something, or it’s not quite clear what the author wants - Duck Learns to Take Cover
  • @ Duck - "It's an industry term." - "My cousin Vinny" - Igor
  • one
    A. Got it. Well then, like this: if (myBasketKey [i] [hash] && myBasketKey [i] [hash] .color) - Duck Learns to Hide

1 answer 1

Why a mistake?
Because you have an array of objects containing objects. Accordingly, myBasket[0]["hash2"] - undefined. Well, that is, there is no property in your first object with the key "hash2", "hash1" only.

Attempting to access the property of a variable with a value of undefined (something like undefined.myProperty ) causes an error in js like:

TypeError: Cannot read property 'myProperty' of undefined

This is solved by a standard trick:

 if (myBasketKey[i][hash] && myBasketKey[i][hash].color === "GREEN"){ //dosmth } 

The point is that in js, as in most other languages, the right-hand side of this logical expression does not hold if the left-hand side clearly understands that it is false.

Here a little complicates the understanding that you have some kind of over-complicated structure at all. In the example, an array of flat objects is sufficient, and not an array of objects containing objects.

  • thank you very much ! helped a lot! and you can find out why he cursed? - user3319778 pm
  • @ user3319778, updated the answer. - Duck Learns to Hide
  • everything is clear and understandable, thank you very much !!! By the way, the structure is such because I save these objects to the local storage, but there was a problem that when adding objects to the local storage in a random order, he automatically sorted them and put them in sequence when I needed them to remain in the order of adding there ( chaotic order), so I decided to throw them into an array, I have a hash is always a unique value, so I decided to wrap the object in another object, therefore - user3319778
  • In general, your answer helped - thanks, but there is a bug, if there is no "green" color in the object, then the default block will not work - user3319778
  • @ user3319778, not, this is not my bug) Default block - it works depending on the value of the expression in switch (); Well, that is, I did not try to fix everything with you, but only the described problem - Duck Learns to Take Cover