Search code examples
javascriptextend

adding an object into another object that is in another object


Say that there are three objects, a note, a noteState, and history:

var note = {
    text: "hello new world"
}

var history = {}
var noteState = {}

how would i be able to add note into noteState, and then add noteState into history?

could i potentially do something like:

$.extend(noteState, {"note1": note});
$.extend(history, {"noteState1": noteState});

and if I were to refer back to it to grab the text from note, how would i go about doing so?

history.noteState1.note1.text  // ?

or is there another method to go about this? thank you!


Solution

  • Why don't you simply

    var note = {
        text: "hello new world"
    }
    
    var history = {}
    var noteState = {}
    
    noteState.note = note;
    history.noteState= noteState;
    alert(history.noteState.note.text);//alerts hello new world