6

I want to check in my function if a passed argument of type object is empty or not. Sometimes it is empty but still not null thus I can not rely on null condition. Is there some property like 'length'/'size' for flex objects which I can use here. Please help. Thanks in advance.

Jacob Poul Richardt
  • 3,145
  • 1
  • 26
  • 29
Ashine
  • 4,517
  • 3
  • 24
  • 26
  • I retagged your question with collections. You question is a bit vague, maybe you can clearify it to get more/better answers. Are using an Object instance as an associative array? If you need a count, maybe you use the Array class instead. If you describe the context a bit better it will be easier to answer. – Jacob Poul Richardt Oct 27 '09 at 00:39
  • If you use an associative array, you get exactly the same thing as an object. The length property will always return 0 no matter how make keys you fill. – sharvey Oct 27 '09 at 13:53
  • I meant not using a and associative array, but changing the code to use a normal array. An associative array indicates that you know which values are stored, and therefor you would not normally need to count the. But it is hard to say without more detail. – Jacob Poul Richardt Oct 27 '09 at 18:45

8 Answers8

15

If you mean if an Object has no properties:

var isEmpty:Boolean = true;
for (var n in obj) { isEmpty = false; break; }
Simon Buchan
  • 12,707
  • 2
  • 48
  • 55
  • Thanks for the help, it was really useful but still can't we have some builtin method like : "myObj.length"/'myObj.isEmpty' Thanks again. – Ashine Oct 26 '09 at 10:40
  • 1
    You are gonna have to extend the `Object` class or the `Dictionary` class to get an "inbuilt" method for that :) – Amarghosh Oct 26 '09 at 11:27
  • There is monkeypatching, but that breaks for .. in loops, which... well... you know. – Simon Buchan Oct 28 '09 at 04:26
  • @Bob: `ObjectUtils` is not a standard API, but yes it would be preferable to patching `Object`. It is actually possible to have a top-level function (put `package foo { public function bar() { ... } }` in a file `foo/bar.as`), which I stylistically prefer to `FooUtils` classes. – Simon Buchan Jan 08 '14 at 03:07
  • What do you mean by non-standard? http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/utils/ObjectUtil.html – Bob Jan 08 '14 at 08:33
  • 1
    @Bob: Ahh, I was looking for ObjectUtil*s*. – Simon Buchan Jan 27 '14 at 04:32
  • Wow sorry, didn't catch that! – Bob Jan 28 '14 at 07:10
6

This is some serious hack but you can use:

Object.prototype.isEmpty = function():Boolean {
    for(var i in this)
        if(i != "isEmpty")
            return false
    return true
}

var p = {};
trace(p.isEmpty()); // true
var p2 = {a:1}
trace(p2.isEmpty()); // false
sharvey
  • 7,635
  • 7
  • 48
  • 66
  • wow, that's an interesting trick. You just sent me on a quest to learn about the prototype member. Not sure how I might use it, but it's an interesting bit of guts to know about. – JStriedl Oct 26 '09 at 19:00
  • I try to avoid prototypes, because the flex compiler complains alot. And I must admit that I'm an auto-completion addict... – sharvey Oct 26 '09 at 20:37
  • possibly check using `this.hasOwnProperty(i)`, in case of other prototype members. – Simon Buchan Oct 28 '09 at 04:28
5

You can also try:

ObjectUtil.getClassInfo(obj).properties.length > 0

The good thing about it is that getClassInfo gives you much more info about the object, eg. you get the names of all the properties in the object, which might come in handy.

Robert Bak
  • 4,246
  • 19
  • 20
2

If object containes some 'text' but as3 doesn't recognize it as a String, convert it to string and check if it's empty.

var checkObject:String = myObject;

if(checkObject == '')
{
  trace('object is empty');
}
Taro
  • 21
  • 1
0

Depends on what your object is, or rather what you expect it to have. For example if your object is supposed to contain some property called name that you are looking for, you might do

if(objSomeItem == null || objSomeItem.name == null || objSomeItem.name.length == 0)
{
 trace("object is empty");
}

or if your object is actually supposed to be something else, like an array you could do

var arySomeItems = objSomeItem as Array;
if(objSomeItem == null || arySomeItems == null || arySomeItems.length == 0)
{
  trace("object is empty");
}

You could also use other ways through reflection, such as ObjectUtil.getClassInfo, then enumerate through the properties to check for set values.... this class help:

import flash.utils.describeType;
import flash.utils.getDefinitionByName;

public class ReflectionUtils 
{
    /** Returns an Array of All Properties of the supplied object */
    public static function GetVariableNames(objItem:Object):Array
    {
        var xmlPropsList:XMLList = describeType(objItem)..variable;
        var aryVariables:Array = new Array();
        if (xmlPropsList != null)
        {
            for (var i:int; i < xmlPropsList.length(); i++)
            {
                aryVariables.push(xmlPropsList[i].@name);
            }
        }

        return aryVariables;
    }

    /** Returns the Strongly Typed class of the specified library item */
    public static function GetClassByName($sLinkageName:String):Class
    {
        var tObject:Class = getDefinitionByName($sLinkageName) as Class;
        return tObject;
    }

    /** Constructs an instance of the speicified library item */
    public static function ConstructClassByName($sLinkageName:String):Object
    {
        var tObject:Class = GetClassByName($sLinkageName);
        //trace("Found Class: " + tMCDefinition);
        var objItem:* = new tObject();
        return objItem;
    }

    public static function DumpObject(sItemName:String, objItem:Object):void
    {
        trace("*********** Object Dump: " + sItemName + " ***************");
        for (var sKey:String in objItem)
        {
            trace("    " + sKey +": " + objItem[sKey]);
        }
    }
    //}
}

Another thing to note is you can use a simple for loop to check through an objects properties, thats what this dumpobject function is doing.

JTtheGeek
  • 1,707
  • 14
  • 17
0

You can directly check it as follow,

var obj:Object = new Object();
if(obj == null)
{
//Do something
}
Kans
  • 13
  • 2
0

I stole this from a similar question relating to JS. It requires FP 11+ or a JSON.as library.

function isEmptyObject(obj){
    return JSON.stringify(obj) === '{}';
}
Eddie
  • 1,428
  • 14
  • 24
-1

can use use the hasProperty method to check for length

var i:int = myObject.hasProperty("length") ? myObject.length: 0;
AndrewB
  • 962
  • 1
  • 10
  • 18