flex - Combine/merge Dynamic Objects in AS3 -
i have 2 dynamic objects , want build 1 contain properties:
var o1:object = {prop1:val1,prop2:val2,prop3:val3}; var o2:object = {prop3:val3a,prop4:val4};
and need obtain third object looks that:
{prop1:val1, prop2:val2, prop3:val3a, prop4:val4};
basically need way iterate through object properties , add new properties third object. have mention i'm quite new as3/flash/flex.
first question, mean have prop3 in both objects? need decide in case of collision that, object has precedence.
secondly, check out introspection apis: http://livedocs.adobe.com/flex/3/html/help.html?content=usingas_8.html
something should work:
public function mergedynamicobjects ( objecta:object, objectb:object ) : object { var objectc:object = new object(); var p:string; (p in objecta) { objectc[p] = objecta[p]; } (p in objectb) { objectc[p] = objectb[p]; } return objectc; }
if property exists in , b, b's overwrite a's. note if values of property object, pass reference, not copy of value. might need clone object in cases, depending on needs.
note: haven't tested above, should close. let me know if doesn't work.
updated fix errors. glad works though.
Comments
Post a Comment