javascript - How to add an object into an object -
i trying add object object.
my previous post helped me on array issue need add object object too.
i have
var temp = {}; for(var i=0; i<test.length; i++){ console.log(test[i]) console.log(product[i]) temp.test[i] =product[i]; } both of console.log show values. however, getting
"uncaught typeerror: cannot set property '0' of undefined" temp.test[i] =product[i]
can me out on this? lot
var temp = {test:[], product:[]}; var test = ['a','v']; var product = ['a2','v2']; for(var i=0; i<test.length; i++){ console.log(test[i]) console.log(product[i]) temp.test.push(product[i]); } you need define property test , product first , push data.
but, not have loop insert every values. can set whole collection in 1 shot.
var testcollection = ['value 1', 'value 2']; var productcollection = ['value 10', 'value 20']; var temp = { test:testcollection , product:productcollection };
Comments
Post a Comment