How to read Object? [duplicate] - javascript

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);

Related

How to update a javascript/json object based on a string defining path of field to be inserted to object [duplicate]

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 3 years ago.
My object is already defined.
I want to insert variables at particular indexes in the object.
Given message object:
message=[{
person:{
actions:[{test:1},{test:2}]
},
person:{}
},
{
person:{
actions:[{test:3},{test:4}]
},
person:{}
}]
1) If my string = "message[0].person.actions[1].visible".
This means I need to insert visible=null at index=1 of actions.
So message object becomes:
message=[{
person:{
actions:[{test:1},{test:2,visible:null}]
}
},
{
person:{
actions:[{test:3},{test:4}]
}
}]
2) If string="message[1].person.accessible",
So message Object become:
message=[{
person:{
actions:[{test:1},{test:2}]
}
},
{
person:{
accessible:null,
actions:[{test:3},{test:4}]
}
}]
I thought of doing split('[') and get that index and insert in that object.
Let me know if there are any libraries out there simplifying this for more complex data structure.
This question got closed along with a link to question [Accessing nested JavaScript objects with string key
My question is different than above mentioned link. I am asking about inserting a new field at particular location in object.
Just mutate the object by assigning it a property.
message[0].person.actions[1].visible = null;
This should work.

Read nested value from json [duplicate]

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

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;

Get key from JSON object [duplicate]

This question already has answers here:
Javascript get object key name
(8 answers)
Closed 5 years ago.
I'm using the flat-cache NPM package and I'm currently blocked because I can't recover the key from the data I'm caching. It must be very simple, but I'm starting with the JS and I'm pulling my hair out on this problem.
Simple example of code :
main.js
var flatCache = require('flat-cache')
flatCache.setKey('d86f003c-bf0a-4b08-9744-1081c78ece9d', {"creation":"2018/02/20", "link":"https://www.npmjs.com/package/uuid","comment":"UUID", "tags":["NPM", "UUID"]});
var a = flatCache.all();
console.log(a);
Example of data from console :
{
"d86f003c-bf0a-4b08-9744-1081c78ece9d": {
"date":"20180220",
"comment":"Hello world",
"tags":[
"hello",
"worlds"
]
}
}
What would be the procedure to follow to retrieve the key : d86f003c-bf0a-4b08-9744-1081c78ece9d ?
Thank you in advance for your answer !
Use Object.keys method.
In your case:
// `a` is defined somewhere there
...
Object.keys(a); // an array of object keys - but only the first level
console.log(Object.keys(a)[0]); // should log `d86f003c-bf0a-4b08-9744-1081c78ece9d`
for further reference - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

Categories

Resources