I have a x
variable with the following content inside:
x = [ {
id: '1',
name: 'name,
} ]
And I thought that to access id
was x.id
, but it is not so. How to access?
I have a x
variable with the following content inside:
x = [ {
id: '1',
name: 'name,
} ]
And I thought that to access id
was x.id
, but it is not so. How to access?
There you have an object inside an array . First you access the index of the element (in case it only has one, then it is 0), then it accesses the member of the object, as it did correctly.
x = [ {
id: '1',
name: 'name',
} ]
document.body.innerHTML += x[0].id;
In the assignment of a value the brackets delimit an array . The keys delimit an object.
Just as curiosity would also work x[0]["id"]
since in JavaScript an object is actually an associative array .