2

I have a json stracture like:

     var abraham = {

                   wife:{
                    name:"Sarah"
                        },

                  first_born:{
                    name: "Isaac"
                    wife:"Rebekkah",
                    child:"Jacob"

                      },

Now i have a new "key" that I want its "value" to be the value of one of its siblings. Someyhing like:

abraham_first_born_name:  first_born.name, //I get Errors:

I tried:

abraham_first_born_name:  first_born.name, //Error: first_born.name undefined
abraham_daughter_in_law: first_born.wife, //Error: first_born.wife undefined
abraham_grand_child:     first_born.child,  ////Error: first_born.child undefined

And tried:

abraham_first_born_name:  this.first_born.name, //Error: this.first_born.name undefined
abraham_daughter_in_law: this.first_born.wife, //Error: this.first_born.wife undefined
abraham_grand_child:     this.first_born.child,  ////Error: this.first_born.child undefined

And Lastly tried:

abraham_first_born_name:  $(this).first_born.name, //Error: $(this).first_born.name undefined
abraham_daughter_in_law: $(this).first_born.wife, //Error: $(this).first_born.wife undefined
abraham_grand_child:     $(this).this.first_born.child,  ////Error: $(this).first_born.child undefined    
}

How can I access the data of a key such as abraham_first_born_name to be assigned as the value of, first_born.name.... which is a fellow sibling key's??

Please note that before asking this questin I visited the google of hours and tried: How to access Json key inside a json key and json sibling data and Getting json sibling data and How to access key itself using javascript and Accessing siblings in jquery and accessing a siblings in jquery

Thank you for any suggestion.

Community
  • 1
  • 1
Universal Grasp
  • 1,835
  • 3
  • 20
  • 29
  • 1
    You cannot do that in JavaScript object literals. You have to add the properties in separate statements. – Pointy Aug 16 '14 at 15:17
  • How??... any suggestion??? – Universal Grasp Aug 16 '14 at 15:18
  • And [one another example](http://jsfiddle.net/Regent3000/5gcjv64r/) if you want to have up-to-date value like in Explosion Pills's answer, but want to be able to call `JSON.stringify` on `abraham`. – Regent Aug 16 '14 at 18:26

2 Answers2

6

You have to use separate statements:

 var abraham = {

               wife:{
                name:"Sarah"
                    },

              first_born:{
                name: "Isaac"
                wife:"Rebekkah",
                child:"Jacob"

                  }
 };

 abraham.abraham_first_born_name = abraham.first_born.name;
Pointy
  • 405,095
  • 59
  • 585
  • 614
1

You could do this with Object.create, but it would be a little verbose.

var abraham = Object.create(Object.prototype, {
    wife: {value: {name: "Sarah"}},
    first_born: {value: {name: "Isaac"}},
    abraham_first_born_name: {
        get: function () {
            return this.first_born.name;
        }
    }
});

http://jsfiddle.net/n4xqbjdo/

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405