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

4 Answers 4

3

Since it is a nested array.

You have to try like,

alert(solidos[0][0].nome);
Sign up to request clarification or add additional context in comments.

Comments

1

Change:

alert(solidos[0].nome);

To:

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

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.

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.

Comments

Your Answer

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