在 AS3 中组合/合并动态对象

发布于 2024-10-02 13:42:08 字数 404 浏览 2 评论 0原文

我有 2 个动态对象,我想构建一个来包含所有属性:

var o1:Object = {prop1:val1,prop2:val2,prop3:val3};
var o2:Object = {prop3:val3a,prop4:val4};

并且我需要获得看起来像这样的第三个对象:

{prop1:val1, prop2:val2, prop3:val3a, prop4:val4};

基本上我需要一种方法来迭代对象属性并向第三个对象添加新属性。我不得不提的是,我对 AS3/Flash/Flex 还很陌生。

I have 2 dynamic objects and I want to build one to contain all the properties:

var o1:Object = {prop1:val1,prop2:val2,prop3:val3};
var o2:Object = {prop3:val3a,prop4:val4};

and I need to obtain a third object that looks like that:

{prop1:val1, prop2:val2, prop3:val3a, prop4:val4};

Basically I need a way to iterate through the object properties and to add new properties to the third object. I have to mention I'm quite new to AS3/Flash/Flex.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不寐倦长更 2024-10-09 13:42:09

第一个问题,你真的想在两个对象中都有 prop3 吗?您需要决定在发生此类碰撞时该怎么做,哪个对象优先。

其次,查看内省 api: http://livedocs .adobe.com/flex/3/html/help.html?content=usingas_8.html

类似这样的内容应该有效:

public function mergeDynamicObjects ( objectA:Object, objectB:Object ) : Object 
{
    var objectC:Object = new Object();

    var p:String;

    for (p in objectA) {
       objectC[p] = objectA[p];
    }

    for (p in objectB) {
       objectC[p] = objectB[p];
    }

    return objectC;
}

如果属性存在于 A 和 B 中,则 B 的属性将覆盖 A 的属性。另请注意,如果属性的值是对象,它将传递引用,而不是值的副本。在这些情况下,您可能需要克隆该对象,具体取决于您的需要。

注意:我还没有实际测试过上面的内容,但应该很接近。如果不起作用请告诉我。

已更新以修复错误。很高兴它对你有用。

First question, do you really mean to have prop3 in both objects? you will need to decide what to do in case of a collision like that, which object has precedence.

Secondly, check out the introspection apis: http://livedocs.adobe.com/flex/3/html/help.html?content=usingas_8.html

something like this should work:

public function mergeDynamicObjects ( objectA:Object, objectB:Object ) : Object 
{
    var objectC:Object = new Object();

    var p:String;

    for (p in objectA) {
       objectC[p] = objectA[p];
    }

    for (p in objectB) {
       objectC[p] = objectB[p];
    }

    return objectC;
}

If the property exists in A and B, B's will overwrite A's. Also note that if the values of a property is an object, it will pass a reference, not a copy of the value. You might need to clone the object in those cases, depending on your needs.

Note: I haven't actually tested the above, but it should be close. Let me know if it doesn't work.

Updated to fix the errors. Glad it works for you though.

傾旎 2024-10-09 13:42:09

您可以使用索引运算符动态访问/设置对象的属性。 for 循环将迭代属性名称,因此如果将它们放在一起,则以下测试将通过:

    [Test]
    public function merge_objects():void {
        var o1:Object = {prop1:"one", prop2:"two", prop3:"three"}; 
        var o2:Object = {prop3:"threeA", prop4:"four"};

        var o3:Object = new Object();

        for (var prop in o1) o3[prop] = o1[prop];
        for (var prop in o2) o3[prop] = o2[prop];

        assertThat(o3.prop1, equalTo("one"));
        assertThat(o3.prop2, equalTo("two"));
        assertThat(o3.prop3, equalTo("threeA"));
        assertThat(o3.prop4, equalTo("four"));
    }

You can dynamically access/set properties on objects with the index operator. The for loop will itterate over the property names, so if you put it all together, the following test passes:

    [Test]
    public function merge_objects():void {
        var o1:Object = {prop1:"one", prop2:"two", prop3:"three"}; 
        var o2:Object = {prop3:"threeA", prop4:"four"};

        var o3:Object = new Object();

        for (var prop in o1) o3[prop] = o1[prop];
        for (var prop in o2) o3[prop] = o2[prop];

        assertThat(o3.prop1, equalTo("one"));
        assertThat(o3.prop2, equalTo("two"));
        assertThat(o3.prop3, equalTo("threeA"));
        assertThat(o3.prop4, equalTo("four"));
    }
时光病人 2024-10-09 13:42:09

您可以迭代对象属性,例如:

var obj1:Object = new Object();
for(var str:String in obj2){
    obj1[str] = "any value"; // insert the property from obj2 to obj1
}

you can iterate over the object properties like:

var obj1:Object = new Object();
for(var str:String in obj2){
    obj1[str] = "any value"; // insert the property from obj2 to obj1
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文