Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

199
Vistas
How to insert object inside an object in javascript?

I'm trying to insert an object inside another object and with what I tried I wasn't successful in any of the, let's say I want to insert an object inside an object, these are the ways I tried:

var ob = {
 user1: {name: 'chris'}
};

1:
ob['user2']['name'] = 'Luana'; //error

2:
var obA = ob['user2'];
obA['name'] = 'Luana'; //error

3:
ob['user2'] = ['name'] = 'Luana'; //error
 

These are the ways I tried, but since I am not successful, how can I insert other properties, other than this way below?

obA['user2'] = {name: 'Luana'}; // for me this is not what i'm looking for

My wish is to make the code below:

var gentleman = {};
var test = 0;

onclick = () => {
 test = 1;

 gentleman ['names'] [test] = 'I am a gentleman' test
 
 console.log (gentleman);
}

Return this result:

gentleman = {
 names: {
   1: 'Im gentleman 1',
   2: 'Im gentleman 2',
   3: 'Im gentleman 3',
   ...
 }
}

Note What I'm doing is, create a piano and each key prepreced I create a new audio with new Audio and pass the url of this key itself, so far so good, the problem needs some logic to check if the audio is already was called, the logic I thought was the following: When the user clicks on the key I insert into a notes_ object the property with the key of the C or D ... key is the value of the new Audio audio, each key I insert into this same notes_ object that is inside another songs_ object. As I saw in several answers, when adding as mentioned in them, the values ​​that are in the object are reset, but that's not what I want;

Check it out

var songs_ = {
 music1 = 'name',
 music2 = 'name',
 notes_ = {}
};

Each key I press, I'll add a property to notes_ like this:

var key_ = new audio(urlNote);

note 1 C = key_;

notes_ = {
 C = key_, //(here contains the audio of the note itself C)
 C_ = key_,
 D = key_,
 ...
} // the content here should not be changed, just added
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Update after updated question

var gentleman = { names:{}};
var test = 0;
document.getElementById("btn").addEventListener("click", function() {
 test++
 gentleman ['names'] [test] = `I am a gentleman ${test}`
 console.log (gentleman);
})
<button id="btn" type="button">Click</button>

Old answer:

the key I'm trying to enter is a variable eg: var key = 'user'; var keyI = 'name'; ob[key][keyI] = 'name'

Perhaps you meant this?

var obj = {
  user1: {
    name: 'chris'
  }
};
console.log(obj)

let key = 'somekey'
let key1 = 'someotherkey'
let val = 'somevalue'
obj[key] = {[key1] : val}

console.log(obj)

about 4 years ago · Juan Pablo Isaza Denunciar

0

There is another regularly way.

You could try this:

var ob = {
 user1: {name: 'chris'}
};

var ob2 =  {
 user2: {name: 'Luana'}
};
var result = Object.assign(ob, ob2);
console.log(result); 

about 4 years ago · Juan Pablo Isaza Denunciar

0

Not possible in that way.

var ob = {
 user1: {name: 'chris'}
};

//If you wanna do this, it will show you an error since 'user2' does not exists on ob:
ob['user2']['name'] = 'Luana'

// So yo wanna create it first:
ob['user2'] = {name: 'chris'};
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda