PDA

View Full Version : Get all nested properties in a flat list of properties?



abcdef
12 Jun 2010, 11:00 PM
I am looking to do the following:
I have an object with potentially nested properties in it. I have another object with flat properties in it. The first object shares certain property names with the second, and I would like to take the value from the second object for the matched properties and update the corresponding property in the first object with it.

Thanks in advance..

Ant1105
13 Jun 2010, 10:44 PM
Sounds like you are trying to do something like this...



var obj1 = {
firstName: 'Foo',
lastName: 'Bar',
address: {
address1: '1234 Something Lane',
city: 'Some City',
state: 'Some State'
}
};

var obj2 = {
firstName: 'Bar',
lastName: 'Foo'
};

Ext.apply(obj1, obj2);

abcdef
14 Jun 2010, 12:09 AM
Sounds like you are trying to do something like this...



var obj1 = {
firstName: 'Foo',
lastName: 'Bar',
address: {
address1: '1234 Something Lane',
city: 'Some City',
state: 'Some State'
}
};

var obj2 = {
firstName: 'Bar',
lastName: 'Foo'
};

Ext.apply(obj1, obj2);


Thanks for the reply.

Yes that works for flat properties at the same level on both ends. Is there a way to do this:



var obj1 = {
firstName: 'Foo',
lastName: 'Bar',
address: {
address1: '1234 Something Lane',
city: 'Some City',
state: 'Some State'
}
};
var obj2 = {
firstName: 'Bar',
lastName: 'Foo',
address1: 'some thing different',
city: 'something different'
};


and the changed object be:



var obj1 = {
firstName: 'Foo',
lastName: 'Bar',
address: {
address1: 'some thing different',
city: 'something different',
state: 'Some State'
}
};


Is there an Ext ready way of doing this?

Condor
14 Jun 2010, 12:14 AM
No, there isn't. You'll have to do that yourself (e.g. using Ext.apply and/or Ext.copyTo).

abcdef
14 Jun 2010, 9:24 PM
Got it..Thanks..