Asked 7 years ago
4 Feb 2017
Views 1517
lain

lain posted

how to get dynamic property of object in JavaScript ?


var keyword={1:"jquery",2:"js",3:"nodeJs",4:"electron",5:"angular",6:"reactJs"}
console.log(keyword.1);

Uncaught SyntaxError: Unexpected number

how to get object property of by number ?

another scenario ::

for(var i=0;i< items.length;i++) { 
  for(var j=0;j< itemfield.length;j++) {
    var field=itemfield[j].name
 console.log(items[i].field);
 }
}


in above code one object contain property lis to object and another its value which multidimensional array of object.
i try to get property name and than try to use it to get its value from another and its not working

so , how to get dynamic property of object in JavaScript ?
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00


var keyword={1:"jquery",2:"js",3:"nodeJs",4:"electron",5:"angular",6:"reactJs"}
console.log(keyword[1]);


you cant access with keyword.1 but as array do keyword[1]

in another scenario


for(var i=0;i< items.length;i++) { 
for(var j=0;j< itemfield.length;j++) {
    var field=itemfield[j].name
    if (items.hasOwnProperty(field)) {  
       console.log(items[i][field]);
    }
}
}


use array had object than use , it is like multidimensional array
items[i][field]
where items[i] have object it property can be access as like we do with array .


Post Answer