1

Can anyone help me how to loop in an object with the property of that object has another object and so on.

ex.


    var You = {
         child: {
              name: {
                 first: 'John',
                 last: 'Doe'
              }
         },
         extend: function(object) {
               // implementation to extend this current object
         }
    };

    var childrenOf1stWife = {
       child: {
          name: {
             first: 'Steve',
             last: 'Jobs',
             company: 'apple'
          }
       },
       adopted: {
          name: {
             first: 'Bill',
             last: 'Gates',
             children: {
                 son1: 'Foo',
                 son2: 'Bar'
             }
          }
       }
    };

    You.extend(childrenOf1stWife);

P.S. My purpose is to extend the default data; the answer Access / process (nested) objects, arrays or JSON mentioned differs from what I like. The answer only here return an array, or an object. What I would like is to extend the current objects property. So something that exist or not on the current object will be altered or added.

Expected results: property of child of You will be changed with child property of childrenOf1stWife but before doing that, it checks to see if that child property has a property of type 'object', then look again in that object. If the deep object has the same property if not, it will extend again. And so on...

Community
  • 1
  • 1
meetmahpuppy
  • 418
  • 1
  • 4
  • 17
  • Literally the first result. https://www.google.com/search?q=loop+through+objects+in+javascript+with+unknown+deep&oq=loop+through+objects+in+javascript+with+unknown+deep&aqs=chrome..69i57&sourceid=chrome&es_sm=122&ie=UTF-8 – James G. Sep 02 '14 at 23:22
  • recursive function `function a(obj) { if(is_object(obj)) return function a(obj)` – Ryan Sep 02 '14 at 23:24

0 Answers0