This question already has answers here:
Is it possible to add dynamically named properties to JavaScript object?
(20 answers)
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 1 year ago.
I need to do this:
let parallaxBp = 768;
ScrollTrigger.matchMedia({
"(min-width: " + parallaxBp + "px)": function() {
// code
}
});
but doesn't work, it says
, expected
So I can't concatenate that string. How can do it?
Related
This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
How to create an object property from a variable value in JavaScript? [duplicate]
(9 answers)
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 2 years ago.
In JavaScript everything is object like String literal, numbers, Object...
And properties can be added to objects dynamically also, but I am adding property to object which refer to string then it seems it is not working
Following is JS code which I am running
var aVar = "some string";
console.log(aVar);
aVar.name = 'raj';
console.log(aVar.name);
Output...
hi
some string
undefined
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 4 years ago.
I currently do:
function outer(prop_name) {
const tmp = {};
tmp[prop_name] = 'hello world';
foo(tmp);
}
Is there a way of rewriting this as:
foo(<expression>)
using an expression involving prop_name?
You can write it as
foo({ [prop_name] : 'hello_world'});
This question already has answers here:
JavaScript Dynamic Variable Names [duplicate]
(6 answers)
Closed 6 years ago.
Is it possible to use button attribute as a variable name of an array in JavaScript? I tried to do it as follows but it returns syntax error.
var $(this).attr("value") = [];
Is there any way to do this?
var buttonValueAttribute = $(this).attr('value');
This question already has answers here:
How do I enumerate the properties of a JavaScript object? [duplicate]
(14 answers)
Closed 8 years ago.
I am very new to javascript. When I do a typeof variable name, I get object{}. I need to look at the contents of this object. How would I do that in javascript?
You can use developer's console (Chrome console / Firebug / etc) or just convert your object to string:
var objAsString = JSON.stringify(yourVariable);
This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 9 years ago.
I need help, how to insert variable n as XX. Variable n is number, for example 3 and I need to get this number to document.form1.any3.value; is it possible? Thanks for advice
function pokus(n){
var any1 = document.form1.anyXX.value;
}
Use square bracket notation:
function pokus(n){
var any1 = document.form1['any'+n].value;
}