1

I have a multidimensional array in javascript as the code below:

var solidos = [];
solidos[0] = [];
solidos[0].push({
 nome: 'Octaedro regular',
 dimensoes: 'Aresta = 100mm',
 material: 'Placa de alumínio',
 tempo: '2 horas',
 maquinario: 'Dobradeira e Morsa',
 imagem: 'octaedro.gif'
});

When I give a alert in some element of the array, it returns me 'undefined'. Why?


alert(solidos[0].nome);

Results: undefined

asked Feb 4, 2014 at 19:35

4 Answers 4

3

Since it is a nested array.

You have to try like,

alert(solidos[0][0].nome);
answered Feb 4, 2014 at 19:36
Sign up to request clarification or add additional context in comments.

Comments

1

Change:

alert(solidos[0].nome);

To:

alert(solidos[0][0].nome);
V-rund Puro-hit
5,5349 gold badges33 silver badges52 bronze badges
answered Feb 4, 2014 at 19:38

Comments

0

Because it's a 2-dimensional array, as you indicate. The value at index 0 is an array, not an object. You need to access the key of the second array:

alert(solidos[0][0].nome);

Please see this jsFiddle Demo.

answered Feb 4, 2014 at 19:37

Comments

0

See my working jsFiddle demo: jsFiddle

var solidos = [];
 solidos[0] = [];
 solidos[0].push({
 nome: 'Octaedro regular',
 dimensoes: 'Aresta = 100mm',
 material: 'Placa de alumínio',
 tempo: '2 horas',
 maquinario: 'Dobradeira e Morsa',
 imagem: 'octaedro.gif'
});
console.log(solidos[0][0]);
alert(solidos[0][0].nome);

In the firebug console, you can see that logging solidos[0] returns an array and logging solidos[0][0] returns the first element in that array.

answered Feb 4, 2014 at 19:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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