Add JavaScript object to JavaScript object

后端 未结 5 1273
离开以前
离开以前 2020-12-23 19:36

I\'d like to have JavaScript objects within another JavaScript object as such:

Issues:

  - {\"ID\" : \"1\", \"Name\" : \"Missing Documentation\", \"Notes\"          


        
相关标签:
5条回答
  • 2020-12-23 20:12
    var jsonIssues = []; // new Array
    jsonIssues.push( { ID:1, "Name":"whatever" } );
    // "push" some more here
    
    0 讨论(0)
  • 2020-12-23 20:19

    As my first object is a native javascript object (used like a list of objects), push didn't work in my escenario, but I resolved it by adding new key as following:

    MyObjList['newKey'] = obj;
    

    In addition to this, may be usefull to know how to delete same object inserted before:

    delete MyObjList['newKey'][id];
    

    Hope it helps someone as it helped me;

    0 讨论(0)
  • 2020-12-23 20:22

    // Merge object2 into object1, recursively

    $.extend( true, object1, object2 );
    

    // Merge object2 into object1

    $.extend( object1, object2 );
    

    https://api.jquery.com/jquery.extend/

    0 讨论(0)
  • 2020-12-23 20:26

    jsonIssues = [...jsonIssues,{ID:'3',Name:'name 3',Notes:'NOTES 3'}]

    0 讨论(0)
  • 2020-12-23 20:34
    var jsonIssues = [
     {ID:'1',Name:'Some name',Notes:'NOTES'},
     {ID:'2',Name:'Some name 2',Notes:'NOTES 2'}
    ];
    

    If you want to add to the array then you can do this

    jsonIssues[jsonIssues.length] = {ID:'3',Name:'Some name 3',Notes:'NOTES 3'};
    

    Or you can use the push technique that the other guy posted, which is also good.

    0 讨论(0)
提交回复
热议问题