calling function when function name is json value [duplicate] - javascript

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()"

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.

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.

object name is same as variable [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 3 years ago.
Let's say I have an object
const myArray = {
a : "hello"
}
and I have a string with the same name of that object
like
var type ="myArray";
when I do console.log(type);
output: myArray
but I want to out that object to the console which has the same name as the value of variable type.
How should I do that?
Thanks in advance
If it's a global variable, it will be stored in window.
So, you can do something like console.log(window[type]) to access to value.

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.

How to complete a variable name with another variable [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Get global variable dynamically by name string in JavaScript
(6 answers)
Closed 6 years ago.
I have an object that stores 3 variables called text0, text1 and text2 that in turn store one string each. I also have a variable that stores the index of the clicked button, so that if I click on the third button it should fetch the text2.
I was trying something like this:
p.text(myObj.text + btnNumber);
But so far all I get is NaN. Is it even possible to do this way? What am I missing?
Use named indexing of the object to get a variable named property:
p.text(myObj["text" + btnNumber]);
Javascript lets you index properties of an object as if it were a dictionary (indexed via string names).

Categories