This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 7 years ago.
I was trying to "put a function into an object" so I wanted to do something like this but I'm getting errors everywhere.
var someobject = {
makename(1) : null,
makename(2) : null,
makename(3) : null,
makename(4) : null
};
function makename(num) {
return (identifier + ' Bot' + num)
}
In modern (ES2015) JavaScript environments, you can do this:
var someobject = {
[makename(1)]: "foo",
[makename(2)]: "bar"
};
The [ ] wrapper around the property name allows it to be an arbitrary expression. The result of evaluating the expression is interpreted as a string and used as the property name.
var someobject = {}
someObject[makename(1)] = null;
someObject[makename(2)] = null;
someObject[makename(3)] = null;
someObject[makename(4)] = null;
This works everywhere. However, #pointy's solution is nicer!
Related
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 7 years ago.
I know that you can evaluate the value of a property inside of a JS object, like the following:
let object = {
value: 5+5
};
I am wondering if there is any possible way to evaluate the name of an attribute with JS, i.e. achieve the following:
let object;
object[5+5].value = "ten";
As something like:
let object = {
5+5: "ten"
};
Yes in ES2015, no in ES5, but first let's clear one thing up: that's JavaScript, not JSON.
In ES2015 (formerly known as ES6):
var something = "foo";
var object = {
[something]: "bar";
};
alert(object.foo); // "bar"
Inside the [ ] can be any expression you like. The value returned is coerced to a string. That means you can have hours of fun with stuff like
var counter = function() {
var counter = 1;
return function() {
return counter++;
};
};
var object = {
["property" + counter()]: "first property",
["property" + counter()]: "second property"
};
alert(object.property2); // "second property"
JSON is a serialization format inspired by JavaScript object initializer syntax. There is definitely no way to do anything like that in JSON.
Sure. Try this:
'use strict';
let object = {
[5+5]: "ten"
};
console.log(object); // Object {10: "ten"}
This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 7 years ago.
Silly example:
<script>
var a = {
'b' : {
'c' : "success!!"
}
};
var d = 'b.c';
</script>
How could I access success!! if I can't go for the obvious solution a.b.c or a['b']['c'], but instead have to use d? I tried a[d], which doesn't seem to do the trick. I also tried to fiddle with eval(). Is this even possible?
If it's really necessary to have the keys in a string separated with a dot, I would use split and reduce:
var success = d.split(".").reduce(function (obj, key) {
return obj[key];
}, a);
Try splitting
var a = {
'b' : {
'c' : "success!!"
}
};
var d = 'b.c';
var splat = d.split('.');
console.log(a[splat[0]][splat[1]]);
This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 7 months ago.
In PHP I can mess around with variable variables and I'm wondering if I can do the same in JavaScript.
I want to create a new object with a property which's name is based on the value of a variable.
if ( obj.name === 'foo' ) {
var data = { foo: value };
}
if ( obj.name === 'bar' ) {
var data = { bar: value };
}
Is there a shorter way of doing this without using eval()? Something like:
var data = { obj.name: value };
Try this:
var data = {};
data[obj.name] = value;
You can read some more about js objects Here.
Objects in JavaScript are simply hash maps. You can access members by indexing with their names. For your problem you can use
var data = {};
data[obj.name] = value;
I've used this to implement a dynamic dispatch mechanism for arithmetic operations on different numerical types as described here.
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;
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);