Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!