Reputation: 2474
Help would be appreciated,to read key from an object, the key is separated with space
var Obj = {
student class: '4th',
student roll: '24'
}
console.log(obj.student class) ? throws error.
console.log(obj.student roll) ? throws error.
Upvotes: 1
Views: 78
Reputation: 761
There are two way to read fields within the object
To access fields having spaces you have to use second option
console.log(Obj['student class']); console.log(Obj['student roll']);
Upvotes: 0
Reputation: 4091
Put it in quotes and use bracket notation.
var Obj = {
'student class': '4th',
'student roll': '24'
};
console.log(Obj['student class']);
console.log(Obj['student roll']);
Upvotes: 3