3

I have a Array of objects which is something like this :

SomeObject (Array)
  [0] (object)
      id = 1
      name = 'one'
  [1] (object)
      id = 2
      name = 'two'

I need it to be An Array of arrays , something like this :

someobject (array)
  [0](array)
     id = 1
     name = 'one'
  [1](array)
     id = 2
     name = 'two'

If I do :

test:Array = someobject as Array 

This only converts the top not the inner objects. If I try to loop through it and make individual arrays 'as arrays' it gets null.

Any Ideas?

4 Answers 4

1

You need an array, containing arrays, each of them contains single object? Then you have to create new array and push into it new arrays. Type cast cannot convert nested objects.

var mainArray:Array = SomeObject as Array
var newArray:Array = [];
for each (var o:Object in mainArray) {
    newArray.push([o]);
}
Sign up to request clarification or add additional context in comments.

Comments

0

as doesn't convert anything at all.
also, there is no point in doing, what you're doing. when working with string keys, there is no difference between Object and Array. so why would you want to do that?

greetz
back2dos

2 Comments

There are differences. Try: var a=new Array(); a["foo"]="bar"; var str=JSON.stringify(a). str will be [].
@kolar: The difference you are referring to is not inherent to Array and Object, but is subject to what kind of JSON serializer you are using. If transmitting arrays with non-numeric keys is a requirement, you could detect those in the serializer and process them as though they were objects.
0

No, I think that is not what was meant...

It is basically a conversion from objects to arrays. That will be:

var myTestArr:Array = new Array();
// FILL IT (for testing)
var myTestArr2:Array = getArrofArr(myTestArr); // This will be the result

function getArrofArr(inArr:Array):Array {
    var retArr:Array = new Array();
    for (var entry in inArr){
        var addArr:Array = new Array();
        for (var entryKey in inArr[entry]){
            addArr[entryKey] = inArr[entry][entryKey];
        }
        retArr[entry] = addArr;
    }
    return retArr;
}

Comments

0

I see you want arrays, but what if I'll propose you a Dictinoary instead?

var someObject:Array=new Array();
for(var i:int=0;i<3;i++)
{
   var d:Dictionary=new Dictionary();
   d["id"]=i;
   d["name"]="name"+i.toString();
   someObject.push(d);
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.