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;
Related
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);
Outside my scope I have:
var imageData = {};
Then I iterate over my data with jQuery .each like such:
$.each(imgData, function(imagesI) {
imageData = imagesI.QuoteImage;
console.log(imagesI);
})
So when I console log imagesI I get back so many objects (it's different each time) for example my console log looks like;
Object {theImage: "select_q26", Info: "Some info", InfoMeaning: "Info Meaning"}
However, whenever I console.log my object outside which is imageData I only ever get the last object I get from iterating over my data.
Is there anyway to add each object I get back to the variable above without overriding the last one I added?
Thanks!
You could use an array and push the values to that
var imageData = [];
$.each(imgData, function(imagesI) {
imageData.push( imagesI.QuoteImage );
});
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])
I have a JSON collection produced from an object graph. Shown below is an example value. I am having trouble accessing the nested 'Type' object to retrieve any of it's values.
[{"Id":1,"Name":"My Name","Type":{"Id":1,"Name":"my Value"}}]
I am using a JS component that has a property that can be assigned a value similar to below.
myProperty: Type.Name, //Not working
Can someone recommend how I set this value?
What you have is a JavaScript array, not an object, and certainly not JSON. So if you have
var arr = [{"Id":1,"Name":"My Name","Type":{"Id":1,"Name":"my Value"}}]
you'd need to index it, and grab the Type object off of that.
var typeName = arr[0].Type.Name;
Im updating a part of an object and the update works fine.Its when I did a console.log on the object before the I called the update functio,n the object was already updated.I was expecting to see the old copy of the object,i know Im doing something really silly.I just want to understand why this is happening.Here is my code
function updateObject(o){
o.a='oneHundred';
o.b='twoHundred'
}
var obj={
a : 'one',
b : 'two',
c : {
a : '',
b : ''
}
}
console.log(obj);//outputs the updated object before I call updateObject()
var upObject = obj.c ;
updateObject(upObject);
console.log(obj);
Chrome (and possibly firebug) doesn't actually log the current state of an object until you observe it. If you put a breakpoint in the code like so:
console.log(obj);//outputs the updated object before I call updateObject()
debugger; // force breakpoint
var upObject = obj.c ;
updateObject(upObject);
console.log(obj);
and then expand the object you will see that it's the un-updated version. You're better off logging the specific properties you want to see rather than entire objects when you can.
Chrome will display the object itself in the console, not a representation of the object at that point in time. You can call stringify to get a snapshot and log that so it won't change when you change the object.
console.log(JSON.stringify(obj));