javascript - How to add an object in an array? -


this question has answer here:

i trying push object array

i have these

for(var i=0; i<test.length; i++){     console.log(test[i])     console.log(items[test[i]])     productobj.push( { test[i] : items[test[i]]} ); } 

both console.log show stuff have error on push(). error "uncaught syntaxerror: unexpected token ["

can me weird issue? thanks!

object literal keys need constant. test[i] isn't going work. you'll need create empty object , add key/value pair:

var o = {}; o[ test[i] ] = items[test[i]];  productobj.push( o ); 

Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -