开发者

Converting array of objects to array of arrays

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?


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]);
}


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


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;
}


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜