0

I'm trying to convert following string to an object considering the object in the string exists:

var exampleStr:String = "myObject.property1.property2";
var exampleObj:Object = exampleStr as Object;

How would I achieve this, and is there an alternative to a method you can provide?

3
  • Did you mean to not include the "" quotation marks around myObject.property1.property2?
    – Marty
    Feb 14, 2014 at 1:44
  • Quotation marks are meant to be there. I'm trying to evaluate if dynamically created object is missing any of chained properties.
    – Kirill
    Feb 14, 2014 at 1:49
  • I see what you mean now - you can achieve that using recursion.
    – Marty
    Feb 14, 2014 at 1:54

3 Answers 3

1

You can use a simple function like this:

function getRValue(target:Object, chain:String):*
{
    for each(var i:String in chain.split("."))
    {
        if(target.hasOwnProperty(i)) target = target[i];
        else
        {
            // Couldn't find property.
            throw new Error("Property " + i + " does not exist.");
        }
    }


    return target;
}

With a test:

var myObject:Object = {
    property1: {
        property2: "Hello world!"
    }
};

trace( getRValue(this, "myObject.property1.property2") ); // Hello world!
trace( getRValue(myObject, "property1.property2") ); // Hello world!
1
  • Thanks for sharing this Marty! This is a good way to accomplish this as well.
    – Kirill
    Feb 14, 2014 at 2:11
1

I presume you are trying to dynamically create an object from an assembled string? If so, I think what you are looking for is:

var exampleObj:Object = this[ "myObject.property1.property2" ];

This presumes that myObject's scope is "this", and that property2 is an Object as well.

2
  • Good example, but for dynamically created object it will throw an error 1069 for property not found in the working class.
    – Kirill
    Feb 14, 2014 at 1:55
  • Well, as long as property1 and property2 exists that code would be fine. The OP's question is more clear after his comments. I didn't realize the goal was to confirm the existence of the properties. :)
    – Ribs
    Feb 14, 2014 at 18:08
1
   //Convert the string to  a property array       
    var t:Array = exampleStr.split(".");

    var exampleObj:Object = null;

    if (t.length > 0)
    {

        var objName:String = t[i];

        //get the object in the class
        exampleObj = this[objName];

        for (var i:int = 1; i < t.length; i++)
        {
            var propertyName:String = t[i];

            if (exampleObj.hasOwnProperty(propertyName))
            {
                exampleObj = exampleObj[propertyName];
            }
            else
            {
                break;
            }
        }
    }
2
  • 1
    Instead of var propertyName:String = t[0]; it should be var propertyName:String = t[i];
    – Kirill
    Feb 14, 2014 at 2:20
  • Yes, it should be t[i].
    – Pan
    Feb 14, 2014 at 4:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.