To add value in arrays (in brackets []) use $objeto.push('novoValor');
How do I add values in variables between braces (empty object)?
Example:
$objeto = {}
add $objeto = {"conteudo":"dado"}
To add value in arrays (in brackets []) use $objeto.push('novoValor');
How do I add values in variables between braces (empty object)?
Example:
$objeto = {}
add $objeto = {"conteudo":"dado"}
$objeto = {};
$objeto.conteudo = [];
for(var i = 1; i < 4; i++) {
$objeto.conteudo.push({"dado":"" + i});
}
console.log($objeto.conteudo);
$objeto = {};
$objeto.conteudo = "dado";
alert($objeto.conteudo);
JavaScript is a dynamic language, that is, you can define new properties for an object at any point in the execution.
So, to add the value it refers to, just assign it directly to the property:
$objeto.conteudo = "dado";
If you already have a variable with an empty object you can simply do
obj.propriedade = valor;
If you're starting all you can do:
var obj = {propriedade: valor};
If the property (s) are dynamic, ie the name of the property / key itself is inside a variable, then you can do this:
var prop = 'minhaPropriedade';
var obj = {[prop]: 12345};
console.log(obj.minhaPropriedade); // dá 12345