Pergunta

So I have stupid question. If I have added A array to other B array, how can I control A array's elements?

I mean If I have Array A (length 4):

Jennifer Jessica John Peter

To Array B I have pushed some data (arrB.push(item1); (arrB.push(item1);..., so It looks like (length 6):

test1 test2 test3 test4 test5 test6 

After It, I have pushed Array B to Array A arrA.push(arrB); Now Array's A length is 5 and It looks like:

Jennifer Jessica John Peter (test1 test2 test3 test4 test5 test6) 

All this data test1 test2 test3 test4 test5 test6 from array B is like 5th element of Array A.

If I use trace(arrA[4]); It returns me test1 test2 test3 test4 test5 test6 But for example how can I get test2? I've tried trace(arrA[4].arrB[1]);, but It returns me undefinied.

Is It possible at all?

Foi útil?

Solução 2

This isn't a traditional multi-dimensional array. For example arrA[2][1] in my example below will give you o the second letter of John not an index into a second array.

Instead you have an array that has multiple elements, one of which is an array object. To access test2 you would use arrA[4][1]. See this fiddle.

var arrA = ['Jennifer','Jessica','John','Peter'];
var arrB = ['test1','test2','test3','test4'];
arrA.push(arrB);
arrA.push('fred');
console.log(arrA);
console.log(arrA[4][1]);
console.log(arrA[5]);
console.log(arrA[2][1]);

The output will be the array then test2 then fred, then o.

This makes a it a bit tricky when you traverse the array in a generic manner, you will need to check whether you are dealing with a string or a string array. Was you intention a multi-dim array or a collection of objects some which are strings and some which are string arrays?

Outras dicas

You'd access it like this:

trace(arrA[4][1]);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top