I do not see the error to access a property of this object:
console.log(routes);
[Object { bounds=((-34.76335, -58.21068), (-34.749880000000005, -58.202540000000006)), copyrights="Datos de mapas ©2016 Google", legs=[1], más...}]
console.log(routes.legs);
undefined
or console.log(routes["legs"]);
is similar: undefined
What am I doing wrong? Thanks
As your console printed out, routes is an array containing an Object, so you could try adding an index before selecting a key in the object.
So, for example:
console.log(routes[0]["legs"]);
// or
console.log(routes[0].legs);
Related
This question already has answers here:
How to get objects value if its name contains dots?
(4 answers)
Closed 3 years ago.
I'm trying to access the value of a key inside a Javascript object. My object currently looks like:
const options = {
"account.country": getCountry,
"account.phone": getPhone,
}
When I console.log(options), it shows the whole object. But, when I try
console.log(options.account) // undefined,
console.log(options.account.country) // error.
I don't understand why. I also tried:
const parsedObj = JSON.parse(options);
console.log(parsedObj);
But it just returns
'Unexpected token o in JSON at position 1'
You should use Bracket Notation when you want to access the property from a string.
const options = {
"account.country": 'getCountry',
"account.phone": 'getPhone',
}
console.log(options['account.country'])
The . in object key name is causing this issue. When you do:
options.account it returns undefined
^ that is because there is no key with name account
when I console log 'options.account.country' it errors
^ that is because it tries to look for key country on undefined (options.account)
You can solve this by using array indexing syntax as follow:
options['account.country']
options['account.phone']
let options = {
"account.country": "getCountry",
"account.phone": "getPhone"
}
console.log(options["account.country"])
const options = {
"account.country": 'getCountry',
"account.phone": 'getPhone',
}
You can access the desired value using options['account.country']
When I console.log 'options', it shows the whole object
Because it is an object
when I console.log options.account it returns undefined
Because there is no account property.
Only account.country and account.phone. So you have to access properties with those explicit names, like this:
console.log(options['account.country']);
But it just returns 'Unexpected token o in JSON at position 1'
It's an object, not a string of JSON.
I have problem to push some value that assigned to be an array in the object
Here my code :
var hasil = [{ product: 'listBarang[i][0]',
shoppers: [],
leftOver: 'listBarang[i][2]',
totalProfit: 0
}]
What I think is using push method like below
hasil.shoppers.push('test')
But it give me an error like this
TypeError: Cannot read property 'push' of undefined
Is anyone know how to deal with this?
hasil is an array of objects, so if you want to maniputate on those objects, you need to access them directly i.e. with hasil[0]:
hasil[0].shoppers.push('test')
I have the following object(mainObj) that get displayed in my console as follows
Object {}
vObjects:Array[12]
videos:Object
__proto__:Object
How can I get the child (vObjects) from the mainObj so I can assign it to it's own variable?
var vObjects = mainObj.vObjects;
I am trying to access some data from an object as follows:
var summaryChanges = {
dataToAdd:[
{name:[]},
{events:[]},
{emails:[]}
],
dataToRemove:[
{name:[]},
{events:[]},
{emails:[]}
]
}
i am trying to log the contents of the name property of data to add as follows:
console.log($(summaryChanges.dataToAdd.name)[0]);
however the console only logs undefined.
dataToAdd is an arrary not an object , so access it like
console.log(summaryChanges.dataToAdd[0].name[0])
You need to realize some things
$(summaryChanges.dataToAdd.name) you are creating a jQuery Object.
summaryChanges it's an object so you can do sumaryChanges.dataToAdd
dataToAdd it's an array, so for get a value you access it like this dataToAdd[index]
At the end you access it like this
console.log(summaryChanges.dataToAdd[index].name[index])
So I have a dictionary-like object and an array:
var colors = {"b":color(0, 0, 0)};
var ar=[["b","0","0"],["b","b","0"],["b","b","b"]];
Now, I would like to get the value from the dictionary using the array like so:
colors.ar[0][0]
Which should give me the color black. However, this gives me an error:
Cannot read property '0' of undefined
I believe this is because it's trying to access colors."b" instead of colors.b .
So how can I get the property from the dictionary by using a value from the array?
Thanks!
I get it. What you want is this:
colors[ar[0][0]]
Since ar[0][0] resolves to "b", colors[ar[0][0]] resolves to colors["b"].
If you use dot notation it will try to access colors.ar which is undefined.