Not able to get JSON value dynamically [duplicate] - javascript

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 4 years ago.
I am working on the following snippet. Why am I not able to get the
let node = 'Em2';
console.log(data.node.c2);
work? As you can see I am able to get data while passing data.Em2.c1 but a dynamic format like this let node = 'Em2'; console.log(data.node.c2); I am getting this error
TypeError: Cannot read property 'c2' of undefined
Code:
var data ={
"Em1": { "c1":"#FFF", "c2":"#EEE" },
"Em2": { "c1":"#DDD", "c2":"#ooo" }
}
let node = 'Em2';
console.log(data.Em2.c1);
console.log(data.node.c2);
var data ={
"Em1": { "c1":"#FFF", "c2":"#EEE" },
"Em2": { "c1":"#DDD", "c2":"#ooo" }
}
let node = 'Em2';
console.log(data.Em2.c1);
console.log(data.node.c2);

Use square braces [] to access object member via variable
var data ={
"Em1": { "c1":"#FFF", "c2":"#EEE" },
"Em2": { "c1":"#DDD", "c2":"#ooo" }
}
let node = 'Em2';
console.log(data.Em2.c1);
console.log(data[node].c2);
Similar question: how to access object property using variable

Instead of console.log(data.node.c2), try console.log(data[node][c2])

Related

Javascript object oriented programming showing undefined [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 months ago.
I am new to javascript oop
i have been trying to get the desired value but it gives me undefined
class person {
constructor(isGreat, brain, personality) {
this.isGreat = isGreat;
this.brain = brain;
this.personality = personality;
}
getDetails(req) {
this.req = req;
let dic = {
isGreat: this.isGreat,
brain: `they have ${this.brain} brain`,
personality: `they have ${this.personality} personality`,
};
return dic.req;
}
}
let person0 = new person(true, "massive", "great");
console.log(person0.getDetails("isGreat"));
req can get read but dic.req shows undefined
The problem is console.log shows undefined.
dic doesn't have a req property. If you want to get a property whose name is stored in req variable, use the following syntax:
dic[req]
Just like accessing array members. In fact, a.b and a['b'] are equivalent in JavaScript.
BTW: The assignment this.req = req is not necessary.
You are returning the req property of your object, not the property with the key stored inside req. You need to use square brackets to get the index stored inside req.
return dic[req]
This is because it gets parsed as dic['isgreat'] rather than dic['req'].

JavaScript positional property access in Objects [duplicate]

This question already has answers here:
How to access the first property of a Javascript object?
(23 answers)
Accessing a JavaScript's object property without knowing that property name
(3 answers)
JavaScript: Get first and only property name of object
(7 answers)
How do I access properties of a javascript object if I don't know the names?
(8 answers)
Closed 3 years ago.
I have the following JavaScript object:
{
"_embedded": {
"dealerListItemDToes": [
{
...
},
{
...
}
]
}
}
Property called 'dealerListItemDToes' will always be at the given position in the object but its name can vary depending on the HTTP requests.
How can I access the property 'dealerListItemDToes' and retrieve its content without referencing its name?
Since it's the only property of the _embedded object, you could access the [0]th item in an array of object entries:
const obj = {
"_embedded": {
"dealerListItemDToes": [
{
// ...
},
{
// ...
}
]
}
};
console.log(
Object.entries(obj._embedded)[0]
);
You can try like this
let data = {
"_embedded": {
"dealerListItemDToes": [{
"h": 1
}]
}
}
console.log(data._embedded[Object.keys(data._embedded)[0]])

How to get object values with dynamic variable from json [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 3 years ago.
I want to get the value of an object field from JSON. I have a dynamic variable let check_permission_key = 'ratingscalename'; and have one JSON
let overwrite_key = {
studentimage: 'student',
ratingscalename: 'rating-scale-name',
ratingscale: 'rating-scale-name',
capturesettingset: 'capture-setting-set',
capturesetting: 'capture-setting-set',
eventlog: 'event-log',
goalcategorie: 'goal-category',
userimage: 'user',
datasheetlink: 'datasheet',
datasheetgoal: 'datasheet',
};
let get_value = overwrite_key.check_permission_key;
So I am fetching the data like this, but it is not working can anyone please help me to resolve this issue?
To access the object property value using a variable use Bracket ([]) notation:
let overwrite_key = {'studentimage':'student','ratingscalename':'rating-scale-name','ratingscale':'rating-scale-name','capturesettingset':'capture-setting-set','capturesetting':'capture-setting-set','eventlog':'event-log','goalcategorie':'goal-category','userimage':'user','datasheetlink':'datasheet','datasheetgoal':'datasheet'};
let check_permission_key = 'ratingscalename';
let get_value = overwrite_key[check_permission_key];
console.log(get_value);
Objects in javascript are pretty much arrays with String keys, so you can access a dynamic property like this:
let get_value = overwrite_key[check_permission_key];
You can use eval() function.
let overwrite_key = {
studentimage: 'student',
ratingscalename: 'rating-scale-name',
ratingscale: 'rating-scale-name',
capturesettingset: 'capture-setting-set',
capturesetting: 'capture-setting-set',
eventlog: 'event-log',
goalcategorie: 'goal-category',
userimage: 'user',
datasheetlink: 'datasheet',
datasheetgoal: 'datasheet',
};
let check_permission_key = 'ratingscalename';
let get_value = eval(`overwrite_key.${check_permission_key}`);
console.log(get_value);
JavaScript eval() Function

Node js add dynamic property [duplicate]

This question already has answers here:
Creating object with dynamic keys [duplicate]
(2 answers)
Closed 5 years ago.
let fileName = "test.c";
let testCase = "Case1";
let test = {};
test.fileName = testCase;
console.log(test)
I need fileName property to be dynamic
What is need is, like below
{
"test.c":"Case1"
}
Can any one help me
test.fileName = testCase;
Won't work in this case. Should be
test[fileName] = testCase;
You can use the ES6 computed property syntax:
{
[fileName]: "Case1"
}
This will be interpreted dynamically as:
{
"test.c": "Case1"
}

Javascript - Using Object literal with a number [duplicate]

This question already has answers here:
Using integer keys with dot notation to access property in javascript objects
(3 answers)
Closed 6 years ago.
Let's say I have an object:
var elObject = {
one: {
name: "Oliver"}
}
I can access name by doing elObject.one.name and everything is great, but let's say I have this instead:
var elObject = {
1: {
name: "Oliver"}
}
Suddenly, I can't access name through elObject.1.name anymore since I'm using 1 instead of 'one'. Is there a a special escape or something I'm supposed to use with object literal and digits?
You can declare the plain object with number 1 using it as string. Once is not allowed to have a property name starting with number, you can access it using bracket notation.
Example and findle below.
var x = {
'1' : {
name: 'Joao'
}
};
alert(x);
try {
alert(x['1'].name);
}
catch(e){
alert(e.message);
}
https://jsfiddle.net/b4c34wLv/

Categories

Resources