javascript - Looping an object with an string array -


i'm rather new jquery , don't understand why loop doesn't display object properties.

i grateful if me.

var shop_array = ["title","price","img","text"]; var submit = $(".add").find(":submit"); submit.on("click",function(e){     var elements = $(".add").children(':input');     for(var i=0;i<elements.length;i++){         if($(elements[i]).val()!==""){             var object = '\"'+shop_array[i]+'\"';             console.log(shopcart.shop_values[object])//dosen't display shop_value;             console.log(object);         }     } }); var shopcart= {     shop_values :{         "title":"a",         "price":"b",         "img":"img",         "text":"text"     },     add: function(){      } } 

your problem " put around variable want use access object properties, should not there not in property names

var object = '\"'+shop_array[i]+'\"'; console.log(shopcart.shop_values[object])//dosen't display shop_value; 

should be

console.log(shopcart.shop_values[shop_array[i]])//dosen't display shop_value; 

for current code have worked shopcart need defined as

var shopcart= {     shop_values :{         "\"title\"":"a",         "\"price\"":"b",         "\"img\"":"img",         "\"text\"":"text"     },     add: function(){      } } 

which awful.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -