Read nested value from json [duplicate] - javascript

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 3 years ago.
On the image You can see what I want to read
console.log("exact val = " + json._array.loftybot.spells.name)
and it gives [TypeError: Cannot read property 'spells' of undefined]
You can see in terminal structure of json
How to get into these values?
I want save values from json to array const json = new ObservableArray
And then use all of these values in my program

How about json._array[0].loftybot.spells.name? with an index 0 on your _array and go further on nested json. SEE at MDN

Related

How to get data from Json Object using javascript [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 4 years ago.
I'm getting data as a json object
var data = JSON.parse(JSON.stringify(data));
console.log(data);
On data console I got this. I want to get key and its value .
{lobby: {…}}
lobby:
8jmb9ca3s04c8el4j5sf0d:"AD8BJBkMKCBoYg_qAAAB"
38cjllj78cx0lic58ujxou:"PX51X9z_M34_9BvtAAAD"
ba8gs1y8779kmakdapxk1:"UowsBDCCsZSpojzPAAAA"
Parse JSON to Object var data = JSON.parse(jsonData);
Use Object.keys(data); to get all keys
Use data.key or data['key'] to get key value.

How to read Object? [duplicate]

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 5 years ago.
Say i have object like
var a = {"user":
{'average':
{'score':4
}
}
}
How can I read object value using its keys
Say I have user Object with me and have key "average.score" can I get the value directly?
a.user["average.score"];
//Coming as undefined
a.user["average"]["score"]
// Working as expected : 4
I have the key of "average.score" all together with me want to get the value of score how can I do it directly without splitting the key.
Use a.user["average"].score
var a = {"user":
{'average':
{'score':4
}
}
}
console.log(a.user["average"].score);

How to access object of an object returned by json [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 5 years ago.
in a little laravel application I'm working on I'm returning data from an ajax request like this:
return response ()->json ( $subject::where('id', $subject->id)->with('division')->get(['id', 'name']));
This returned something quite like an object that have nested objects. This how my results looks when I log it to the console.
I want to get the name and id of the subject details returned, which in this case is History and 8. Also I want to be able to access the division array and properties of the object it has.
I do this to log the name of the subject console.log(data.name) but in returned I get:
undefined
How can I achieve this?
You have an array of objects, which has one property (division) - which contains another array. So you have to access the array indices
console.log(data[0].division[0].name);
Looking at the object structure we see that this is an array of elements, so it should be data[0].name to fetch History text
I'm assuming data is your entire object.
Try data[0].name

How to access Json [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 6 years ago.
I am trying to read from a JSON array with a nested array which has its value name spaced out. So I get an error whenever I run the code.
var error = [
{
"LessonName":"Understanding Multiplication",
"LessonID":"13343",
"no of questions":[{"Locked":"31","Unlocked":5}]
},
{
"LessonName":"Finding Unknown Values ",
"LessonID":"13424",
"no of questions":[{"Locked":"34","Unlocked":5}]
}
]
function jsd(){
document.write(error[0].LessonName);
document.write(error[0].'no of questions'[0].Locked);
}
document.write(error[0]."no of questions"[0].Locked); Doesn't seem to display.
You may use a property accessor with brackets for the string.
error[0]['no of questions'][0].Locked
You must use this syntax for strings with spaces.
document.write(error[0]['no of questions'][0].Locked);

How to correctly get JSON value? [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 years ago.
I have this json array
var currencyformats =
{"USD":[
{'symbol':'$', 'left':true}
],
"UAH":[
{'symbol':'₴', 'left':true}
],
"EUR":[
{'symbol':'€', 'left':false}
]
};
How retreive '₴' ?
I tried this (in cookie "to" I've "UAH")
currencyformats[$.cookie("to")].symbol
but I've obtained undefined
The problem is that under each country code, you've defined an array with a single object. That means that after you access the country code, you'll need to also access the first index in the array.
So, assuming the cookie is set to the value that you're expecting:
currencyformats[$.cookie("to")][0].symbol;

Categories

Resources