This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 8 years ago.
I am attempting to add a variable key, with no luck.
Here's what I got so far:
mysql('translations',{
check: 'element_id',
element_id: element_id,
{'lang-'+lang_id}: value
});
The variable key is the last line of the function.
Any ideas?
You can't use expressions for the identifiers in an object literal.
First create the object, then you can use dynamic names:
var obj = {
check: 'element_id',
element_id: element_id,
}
obj['lang-'+lang_id] = value;
mysql('translations', obj);
You can do this:
var x = {
check: 'element_id',
element_id: element_id
};
x['lang-'+lang_id] = value;
mysql('translations', x);
Related
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
I need an object in Typescript declared like this:
queryParameters = { flagged: true };
Now I would like to have the flagged to be retrieved from a variable name. Something like:
var param = 'flagged';
queryParameters = { ValueOf(param): true };
Any idea ?
Thanks.
Why not use computed property names:
queryParameters = { [param]: true };
This question already has answers here:
JavaScript - Why can't I call a variable "name"?
(2 answers)
Closed 5 years ago.
My question is pretty simple. I'm creating two objects. The second object is referencing an object inside the first object.
var me = {
name: {
first: "justin"
}
};
var name = me.name;
console.log(me.name.first); // "justin"
console.log(name.first); // undefined
Why am I getting undefined in my second console log? Shouldn't I get "justin" instead?
You need to use another name. There is a name variable which is global.
var me = {
name: {
first: "justin"
}
};
var anotherName = me.name;
console.log(me.name.first);
console.log(anotherName.first);
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
Imagine I have an object like:
var obj = {
name: {
value: 'Sergio'
},
lastName: {
value: 'Tapia'
}
}
I want to create a function that grabs the value of a given property.
Ideally:
console.log(getProperty(obj, 'name'));
=> 'Sergio'
console.log(getProperty(obj, 'lastName'));
=> 'Sergio'
You can use bracket notation to access the property on the object. Your function would be:
function getProperty(obj, property) {
return obj[property].value;
}
I would probably name it getProperyValue instead.
function getProperty(obj,property){
return obj[property].value;
}
This function should help you achieve what you need.
function getProperty(obj, key){
return obj[key].value;
}
I believe
console.log(getProperty(obj, 'lastName'));
should return 'Tapia' rather than 'Sergio'.
This question already has answers here:
Use a concatenated (dynamic) string as JavaScript object key? [duplicate]
(6 answers)
Closed 8 years ago.
I am trying to add a property to an object, but Dot notation can't handle a string.
my object:
var lists = {
"Cars":{"Ford":false,"Ferarri":false},
"Names":{"John":true,"Harry":false},
"Homework":{"Maths":true,"Science":false,"History":true,"English":true}
}
Adding a property:
function add_item() {
var input = "Alfa Romeo";
var command = eval('lists.Cars.' + input + '=false');
}
How can I do this using Bracket Notation seeing as it's a 2D object?
No need for eval.. and blah is undefined in your example.
var lists = {
"Cars":{"Ford":false,"Ferarri":false},
"Names":{"John":true,"Harry":false},
"Homework":{"Maths":true,"Science":false,"History":true,"English":true}
}
function add_item(key, value) {
lists.Cars[key] = value;
}
add_item('Alfa Romeo', true);
console.log(lists);
This question already has answers here:
How to create an object property from a variable value in JavaScript? [duplicate]
(9 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
I need to figure out how to create a dynamic key string for an object. This expression makes JavaScript complain.
return {$(this).val(): true}; // returns an object e.g. {2: true}
What am I doing wrong?
You have to create the object, then use bracket notation for the dynamic key
var obj = {};
var val = $(this).val();
obj[val] = true;
return obj;
or a completely unnecessary one-liner
return (function(o,e) {o[e.value]=true; return o;})({}, this);
The JavaScript object literal syntax {x: y} specifies that x will be a (possibly) quoteless string, and y any value. You can't use this syntax for dynamic keys.
Use this instead:
var foo = {};
foo[$(this).val()] = true;
return foo;