Nested Objects and function variables [duplicate] - javascript

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 3 years ago.
I am trying to access a nested object based on the name of the object
I have already tried what I did below but it is giving me an error saying that "Cannot read property 'amount' of undefined" Can someone please explain to me why this doesn't work to point me in the direction of an alternative method? Thanks
var test = {
"name": {
"amount": "200"
},
"telle": {
"amount": "150"
}
}
function getIt(testing) {
return test.testing.amount;
//return test.name.amount;
}
console.log(getIt("name"))

function getIt(testing) {
return test[testing].amount;
}
That way you tell js to look for key with name stored inside testing variable.

Related

How to extend variable name with a number? [duplicate]

This question already has answers here:
Getting a Custom Objects properties by string var [duplicate]
(2 answers)
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 4 months ago.
I have an array retrieved from a JSON file and I am using dataArray.forEach(function(e, idx, array) to loop every element and create a table.
I can simply call for each element with console.log(e.id) but how can I list properties with incremental numbers on them without specifying it or its exact the position in loop?
I need to end up with something like this console.log(e.dp_1.value); but instead of this number I wish to use idx. Is this possible?
I have heard there is getChildByName but not sure how to use it and if it fits.
{
"devices": [{
"id": 0,
"sn": "#12BB56CC9",
"dp_1": {
"value": "65345",
"multiplier": 0.001,
"digits": 2
},
"dp_2": {
"value": "55345",
"multiplier": 0.001,
"digits": 2
}
},
// much more like those
]
}

How is possible show JSON value with different way [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 2 years ago.
I wonder how it is possible in a similar situation, the solution
var lang_object = {
"UK": {
"ERROR": {
"fullname_empty": "fullname error",
"phone_empty": "phone error",
}
}
};
I have JSON Object.
var z = 'UK';
console.log(lang_object.z.ERROR.fullname_empty);
this example do not work, why? z = "UK".
var z = eval('UK');
console.log(lang_object.z.ERROR.fullname_empty);
this also dont work.
console.log(lang_object.UK.ERROR.fullname_empty);
this example work
Short answer:
lang_object[z].ERROR.fullname_empty
Long answer:
object.z refers to the value of the key z in the object object.
object[z] refers to the value of a key equals to the value z, in the object object.

My JavaScript function isn't using one of the variables i need it to [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 3 years ago.
I'm creating a mongoose schema and need to do required checks a lot, so I tried creating this function. But it says that 'subcategory' is initialized but never used. How can I get the function to check this. like I need it to?
function requiredCheck(subcategory, value) {
return this.subcategory === value;
}
Use bracket accessors to check a property of this by variable:
function requiredCheck(subcategory, value) {
return this[subcategory] === value;
}
Otherwise you're checking for the property with the literal key "subcategory" which probably isn't what you want.

Get object element from a defined string [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
how to get data from object with define string?
case:
var data = [
{name:"Sharma",country:"India"},
{name:"Udin",country:"Indonesia"},
{name:"John Carter",country:"Mars"}
];
getData(data,"country");
function getData(data,element){
console.log(data[1].element);
}
i want to get country but result is undefinded, how to fix this?
You would need to know both the index and the property
function getData(data,index,element){
console.log(data[index][element]);
}
getData(data,1,"country");
function getData(data,element){
console.log(data[1][element]);
}
That's the correct way to access the value by using a key that is a string.

Accessing element by variable [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
I have an array and want to access its data. However, I need to use a variable instead of the name to access the data.
For example;
My data:
$scope.myData = {
"user": [
{ child[{......}],
..........
}
],"user2": [
{
child[{......}],
.........
}
],...........
The following works
console.log("lenght:"+$scope.myData.user[0].child.length);
but I want to use a variable instead of user[0], because it is dynamic, it changes every time.
Similar to
var m=user;
console.log("lenght:"+$scope.myData.m[0].child.length);
How about this?
var m = 'user';
console.log("lenght:"+ $scope.myData[m][0].child.length );

Categories