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