Chiara Magliacane
Chiara Magliacane

Reputation: 13

How to create multidimensional associative array in javascript

Hy guys,

I am studying javascript.

Is it possible to create this type of array? where product code is replaced by values in particolar my variables cod_prodotto and not in a string like this.

var prezzo = $("input[name='prezzo']").map(function(){return $
(this).val();}).get();
var quantita_magazzino = $
("input[name='quantita_magazzino']").map(function(){return $
(this).val();}).get();
var cod_prodotto = $
("input[name='cod_prodotto_tmp']").map(function(){return $
(this).val();}).get();
  var myArray = new Array();
for (i=0; i<cod_prodotto.length; i++){
myArray[i] = {codice_prodotto:{'cod_prod': cod_prodotto[i], 
'prezzo': prezzo[i], 'quantita': quantita_magazzino[i]}};
}

[{codice_prodotto: {cod_prod: "S10_1949", prezzo: "98", quantita: "7276"}}, {codice_prodotto: {cod_prod: "S10_4757", prezzo: "85.68", quantita: "7302"}}, {codice_prodotto: {cod_prod: "S10_4962", prezzo: "103.42", quantita: "7302"}}] (3) (employer_page.php, line 261)

I would like to understand how to get for example the 98 values'.

Thanks all.

Upvotes: 0

Views: 41

Answers (1)

H.T.
H.T.

Reputation: 476

You can access the array element by its index, and the object value by its name. So you can access "98" value with this code :

arr[0].codice_prodotto.prezzo

Upvotes: 1

Related Questions