Get Value of Child Object with JavaScript - javascript

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;

Related

JavaScript : Printing Dynamic value/data

This is sample JSON I got {"valueofa" : 1234}.To print the value it is something like body.valueofa. However, this valueofa can be anything like apple or something else. So to parse that I object I had tried with ${body}.${value} which isn't working and it's shouldn't be. How can I set the variable with body. So that I can parse the body whatever the value is.
If your object always contains only one key value pair like shown in the question, and you don't care about the key, you can just use Object.values()
console.log(Object.values({"valueofa" : 1234})[0]);
console.log(Object.values({"apple" : 1234})[0]);
The same is true for objects holding multiple key value pairs, Object.values() will return an array of all the values in your object if you omit accessing the first element with [0]
You can access a value of JSON using square brackets.
var value = "valueofa";
body[value];

Why am I not seeing the added property in the object?

I have to add a property for an object returned from my db before pushing it to an array. Changing properties work, but adding new ones doesn't. Can anyone can explain the logic behind this behavior?
ids.forEach(async (id, index) => {
//get object without highlight property
let real_a = await database.getA(id)
real_a.highlight = "shoud add highlight property to real_a object"
realItems.push(real_a)
// the correct string is printed
console.log(real_a.highlight)
//object still doesn't have that property
console.log(real_a)
}
It was the intended behavior.Sorry for bothering.
I was using a mongodb query for the database.getA(id) function and it turns out you have to specify a parameter in the mongodb query to get an actual changeable JSON object.
Here is the complete answer:
Why can't you modify the data returned by a Mongoose Query (ex: findById)

find value in complex object javascript

Basically I have a complex object that retrieves the GPT API (google publisher tag) with this function:
googletag.pubads().getSlots();
The object value is something like this:
I need to know if there is a way to compare the value of each property with an X value without getting a problem of recursivity (because the object is huge and i need to to that validation several times)
Also, I tried to convert that object into a JSON with JSON.stringify(), and then tried to get the value with a regex, faster, but with this option, I have the problem with Cyclic Object Value.
Any suggestions ?
it's more simple. try it to convert it into an array and later use a filter for comparative with your value.
var objGoogle = {};
var arrayObjectGoogle = [objGoogle];
var filter = arrayObjectGoogle.filter(function(obj){
obj.yourAttr == yourValue; });
this will give you a second array with the values found it. later, index the array for pick up the value do you need.

find and replace property value in json object using javascript/underscore.js

I have one list as follows
var data = [{id:1, name:'user1', img:'img1.jpg' },{id:2, 'user2', img:'img2.jpg' }]
Now I have to replace img property with new value (e.g. 'New_Image_2.jpg') for id=2 using underscore.js or in javascript using less effort. right now I have created one small function as follows.
var imgpath = 'New_Image_2.jpg';
var tmpdata = _.findWhere(data,{id:2});
if (tmpdata) {
tmpdata.img = imgpath;
}
Now, in above method, my problem is, How to marge new value with original data?
Well your code should already work because _.findWhere returns the first object corresponding to your constraint or undefined if it does not exist, it means that modifying tmpdata.img actually modifies the value you want to modify.

How to access object of array?

I am new to jquery and trying something and got stuck at it,
My problem is i have object with array in it i am not able to find the way to access that array from the object
//My object is shown in debugging time is as below
cache:object
0001-:Array[2]
0:value1,
1:value2
_prto_:object
and i want to access the value1 and value2 from the 0001- array from that object is there way to access that array. Any help would be great. I know with $.each i can loop through it and and then again access the array but is there any other way to do it.
You can access it like, and keep in mind that you should use bracket notation in this context, since your keys having a starting character as a number.
cache['0001-'][0] //first element on that array
cache['0001-'][1] //second element
A workaround for your new requirement,
var cache = {'0001-' : [0,1]};
var xKeys = Object.keys(cache);
console.log(xObj[xKeys[0]][0]);
console.log(xObj[xKeys[0]][1]);

Categories

Resources