0

I have the following JS structure:

var master = {
    one: 'this is text',
    two: [
        {
            child: '#mydiv'
            child_two: ''
        },
        {
            child: '#mydiv'
            child_two: ''
        },
    ]
};

Is it possible to make child_two be equal to the child value?

Something like this: master.two.child = '#mydiv'

gespinha
  • 7,968
  • 16
  • 57
  • 91
  • 4
    First off, the JS structure you show is not legal javascript. Is `two` supposed to be an object, NOT an array? – jfriend00 Nov 01 '14 at 20:43

1 Answers1

1

No, you'll have to do it in two steps, like this:

var master = {
    one: 'this is text',
    two: {
        child: '#mydiv'
    }
};
master.two.child_two = master.two.child;

alert(JSON.stringify(master, null, 4));

Or alternatively, you could define a variable ahead of time for the identical value, like so:

var myDivSelector = '#mydiv';
var master = {
    one: 'this is text',
    two: {
        child: myDivSelector,
        child_two: myDivSelector
    }
};
Troy Gizzi
  • 2,480
  • 1
  • 14
  • 15