Get object element from a defined string [duplicate] - javascript

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.

Related

When using a variable to call an object from a JSON file, it doesn't call it at all [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 1 year ago.
Using JS in conjunction with a JSON file to turn a room ID into the room information.
function rooms(roomID) {
const roomsDB = require("./rooms.json")
let roomsID = "r".concat("", roomID).toString()
console.log(roomsDB.roomsID)
}
rooms(46)
This is the function I'm using to turn the ID into the info, and the rooms.json file is as follows:
{
"r46":"house"
}
Ideally, this would log house into the console, but I only get undefined.
Why isn't the JSON object being called properly?
To dynamically access an object property by name in a string variable you need to use brackets:
roomsDB[roomsID]
The dot (roomsDB.roomsID) is just like roomsDB["roomsID"], it doesn't substitute the variable.

Why doesn't JavaScript convert a string to an Object? [duplicate]

This question already has answers here:
How to check if object property exists with a variable holding the property name?
(11 answers)
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
I'm attempting to run a list of strings through an object. When I do it individually it works, but when I pass it through as a string it doesn't work. How would I fix this?
// this doesn't work
var a = "IntegrationItem1";
var data = faq.a;
// but this works
var data = faq.IntegrationItem1;
What's causing the first example to not work? Is the variable data seeing it as faq."IntegrationItem1" instead of faq.IntegrationItem1?
You can access properties of the object using it's names:
var a = "IntegrationItem1";
var data = faq[a];
what you need is faq["IntegrationItem1"] => faq[a]

How to use an object's value as the parameter for another object in Javascript? [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 6 years ago.
If I have:
var key = "Section";
course.key = "101";
I get told course.key is unidentified. I'm trying to set course.Section = "101". Is there anyway to pass key's value in the second line?
The property key of course is undefined. Use bracket notation.
course[key] = "101"
is the same as
course["Section"] = "101"

calling function when function name is json value [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
I have a json object like
var json={
key1:val1,
key2:"foo" //as a string
}
and i have foo function
$scope.foo=function{
//something
}
Now on ng-click I want to do something like ng-click="json.key2()"
Basically I want to call this foo function from it's string name ?
Is it possible ?
I know it's bad approach, but is it possible ?
Thanks
You can access properties on an object dynamically by using square-brackets.
For example, in your controller (after declaration of $scope.foo):
$scope.fn = $scope[json.key2];
And in your markup:
ng-click="fn()"

How to get the value dynamically from JSON object [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 7 years ago.
I want to retrieve a JSON value dynamically from JSON object. Below is the code i am using to get the value from JSON object.
var jsonObj = JSON.parse(jsonData);
console.log(jsonObj);
jsonSplit = jsonToFind.split(htmlSplit+".")[1].trim();
console.log(jsonObj+"."+jsonSplit);
But I am getting [object Object].ensighten_tag.
Here ensighten_tag is the dynamic key value.
Can anyone suggest me how to get the value dynamically ?
console.log(JsonObj[jsonSplit])

Categories