Concat string in $scope angularjs [duplicate] - javascript

This question already has answers here:
Variable as the property name in a JavaScript object literal? [duplicate]
(3 answers)
Closed 6 years ago.
I want to pass string name through argument in function and concat in $scope, this is possible?
function test(txt){
$scope.person. + txt; // I want to do $scope.person.status; for example
}
Thanks.

Yes, just do:
function test(txt){
$scope.person[txt];
}

Check Property accessors.
As any object you can access their properties using Dot notation or Bracket notation. When you don't know before hand what property you would be accessing you would use Bracket notation ideally.

function test(txt){
$scope.person[txt] = ''; // We need to assign something to it if not it wont work;
}
Demo example : https://jsbin.com/gihizo/3/edit?html,js,console,output

Related

Value of function parameter is not getting [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 3 years ago.
I'm not getting the actual value of my parameter name in the below function. Object.assign taking the function parameter name as a string literal so the resulted JSON object also named as name
see the below code.
Please see the resulted json object i got.
How to fix this?
wrap it with []
return Object.assign(..., {[name]: JSON.parse(...)})

how to set a variable as a property [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
i'm looking for a way to set a variable as a property (for an element same as a div)
here is the code:
function set_it(element,property){
return element.style.property;
}
console.log(set_it(document.getElementById("div1"),"left"));
at last line i'm trying to get "left" property of "div1" by the function
but I get undefined in console
I know , actually it will look for a property named "property" , which doesn't exist
but how can I do this ?
thanks
function set_it(element,property){
return element.style[property];
}
Like so! You can use variables as object accessors when you use square brackets, not dot notation.

Use a variable to a variable [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
I want to pass a variable into a variable
This is what I want
uk.objects.ARG
I have
var d= ARG
how can I get uk.objects.ARG?
I tried
uk.objects.d
, javascript stores it as the string, not a variable
What should I do in order to make it as a variable ?
I think what you are looking for is perhaps:
var d = "ARG";
uk.objects[d];
You can access JavaScript objects' properties also with the bracket notation:
object[property] = value;
The same works for reading. So in your case you could do the following:
var value = uk.objects[d]

Updating object values in JavaScript via function [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 9 years ago.
In JavaScript I am trying to update an object value through a function, through which I am passing the object property to update.
However, this won't work - and I can see why, but don't know how to combat it!
myObject = {"testItem": "testValue"};
console.log(myObject.testItem);
function updateSomeValue(objectItem, newValue){
myObject.objectItem = newValue;
}
updateSomeValue('testItem', 'newValue');
console.log(myObject.testItem);
Now, I can see the issue here is that in the function, myObject.objectItem is expecting an item in the object called objectItem - it won't translate it to testItem.
How do I do this?
By using a different notation. Using [ .. ] you can specify the property name as a string.
function updateSomeValue(objectItem, newValue){
myObject[objectItem] = newValue;
}

How to use a variable value as a property of an object? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to create an object property from a variable value in JavaScript?
I want to create an object using a value in a variable as the property name.
I have a variable called propertyName:
propertyName = "first";
How do I use the value stored in this variable as object property, as in the following?
obj.first = something; // 'first' should be extracted from propertyName
Use square bracket notation:
obj[propertyName] = something;
This should work:
object[ propertyName ];
It's an alternative form to the dot notation. What sets it apart from it is that it allows you to dynamically generate the property name using strings.

Categories

Resources