1

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
  • 2
    `obA['user2'] = {name: 'Luana'};` is not what you are looking for? Why not if that is how it works – mplungjan Oct 29 '21 at 10:19
  • 1
    var ob = { user1: {name: 'chris'} }; ob = { ...ob, user2: {name: 'Luag'} } console.log(ob) – Vovan_Super Oct 29 '21 at 10:20
  • 1
    Key in the object `ob` is `user1` but you are trying to use `user2` which is undefined. This is why "1" and "2" don't work. – Yousaf Oct 29 '21 at 10:26
  • I rephrased the question to make it a little clearer, sorry – Chris Answood Oct 29 '21 at 12:40
  • So I think you are searching for something like .push() but for Objects right? You could do it as Array and convert it into objects: https://stackoverflow.com/questions/4215737/convert-array-to-object – AndyNope Oct 29 '21 at 14:55
  • @ChrisAnswood - You still haven't answered mplungjan's question. Currently, the question seems to boil-down to "I know how to do it, but I don't feel like doing it this way. Can someone help me find an alternative?" - which, without any justification sounds misguided at best. – enhzflep Oct 29 '21 at 20:52

3 Answers3

1

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)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
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); 
AndyNope
  • 427
  • 6
  • 12
-1

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'};
munleashed
  • 1,677
  • 1
  • 3
  • 9