This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 7 years ago.
var obj = {a:1,b:2};
var str = "a";
console.log(obj.str);
This outputs undefined. What am i missing here ?
You need to use []
var obj = {a:1,b:2};
var str = "a";
console.log(obj[str]);
Related
This question already has answers here:
What does this symbol mean in JavaScript?
(1 answer)
What do square brackets around a property name in an object literal mean?
(2 answers)
Closed 3 months ago.
What is happening in the code on line 10 ({[last]: newObj}) in the following snippet:
How is JS able to use the value of parameter last instead of using last as the property name?
let first = 'first';
let last = 'last';
function foo(first, last) {
let newObj = {
name: 'newObj'
};
let obj = {};
Object.assign(obj, {[last]: newObj});
return obj;
}
console.log(foo('bye', 'hey')); // { hey: { name: 'newObj' } }
Thanks.
This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 1 year ago.
hi what can I use inspite of eval function for that code :
product_pcs = 12;
product_code = "lkb_12" ;
eval(product_code +"_pcs_1 = product_pcs");
alert (lkb_12_pcs_1);
I recommend you build an object instead
const products = {};
prodcuts['lkb_12'].pcs = 12;
prodcuts['lkb_12'].name = 'test';
prodcuts['lkb_13'].pcs = 14;
prodcuts['lkb_13'].name = 'test2';
// ...
``
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 4 years ago.
i want to pass a string to a javascript function and use it as an attribute like this :
function object (){
this.somevalue = 1;
this.somevalue2 = 1;
this.updateAttribute = function(name,value){
this.{name} = value;
}
}
can it be done ? Thank You .
Yes, you can use [] operator bracket notation:
this[name] = value;
This question already has answers here:
Why can't I access a property of an integer with a single dot?
(5 answers)
Closed 6 years ago.
function s(e) {
return e.toString();
}
var a = s(3); // works
var b = 3.toString(); // error
For example, set var a to the return value of s() that returns the first argument.toString(), but you can't set var b to 3.toString()
Javascript is expecting number(s) after the decimal, you can still do this, but you need to put your number in parenthesis:
(3).toString() = "3"
This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Use variable for property name in JavaScript literal?
(3 answers)
Variable as the property name in a JavaScript object literal? [duplicate]
(3 answers)
Closed 8 years ago.
My goal is to dynamically generate variables foo1, foo2 and foo3 and assign bar to them using the following code:
for (var i=1;i<=3;i++) {
myapp.set({ foo+i: "bar" })
}
I tried using eval on foo by it doesn't work. Any ideas?
for (var i=1;i<=3;i++) {
var myObj = {};
myObj['foo' + i] = 'bar';
myapp.set(myObj);
}
You can do this with square brackets. If you want the variables to be in the global scope, then use window['foo'+i].
Eg:
for (var i=1; i<=3; i++) {
window['foo'+i] = 'bar';
// OR, if you want them in 'myApp' scope:
myApp['foo'+i] = 'bar';
}
http://jsfiddle.net/TASfG/
var myApp = {};
for (var i=1; i <= 3; i++) {
myApp['foo'+i] = "bar";
}
console.log(myApp);