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
4 Answers 4
Since it is a nested array.
You have to try like,
alert(solidos[0][0].nome);
Comments
Change:
alert(solidos[0].nome);
To:
alert(solidos[0][0].nome);
Comments
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.
Comments
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.