-3

I've a javascript object inside a variable data like:

data: Object {
   aliquota: 23,
   imponibileLordo: 300,
   imponibileScontato: "",
   imposta: 69
}

In another function i've to iterate the object using an index like:

for ( var index = 0; index < data.length; index++ ) {
    var valueOfIndex = data[index];
    console.log(valueOfIndex);
}

But I get error cause data[0], data[1], data[2], data[3] are not defined.

How can solve this? Important: i've to use a numeric INDEX for cycle for cause I do other operation based on index value.

UPDATE: Ok my goal is another. So i'll upload the script i'm using with the fix I DON'T LIKE but works. (See //ACTUAL FIX comment) Also the fiddle here: https://jsfiddle.net/am3ovL3b/5/

var array_iva = [];

var data = { "impo": 10, "aliq": 20, "other": 30 };
var column = { "title": "TEST 1", "data": data };
array_iva.push(column);

var data = { "impo": 40, "aliq": 50, "other": 60 };
var column = { "title": "TEST 2", "data": data };
array_iva.push(column);

var json_righe = [[ "Imponibile lordo" ], [ "Aliquota %" ], [ "Others" ] ];

for ( var i = 0; i < array_iva.length; i++ ) {

var titolo_colonna_iva = {};

titolo_colonna_iva['title'] = array_iva[i]['title'];

for ( j = 0; j < json_righe.length; j++ ) { //  for each 

    var riga = json_righe[j];

    for ( var k = 0; k < array_iva.length; k++ ) {  //  EMPTY DEFAULT
        riga.push("");
    }

    //  ACTUAL FIX

    if ( j == 0 ) {
        riga[(i+1)] = array_iva[i]['data']['impo'];
    } else if ( j == 1 ) {
        riga[(i+1)] = array_iva[i]['data']['aliq'];
    } else if ( j == 2 ) {
        riga[(i+1)] = array_iva[i]['data']['other'];
    }

      //END ACTUAL FIX

    /*
    THIS DOESn?T WORK
    riga[(i+1)] = array_iva[i]['data'][j];
    */

    json_righe[j] = riga;

}

}
Gianluca Ghettini
  • 11,129
  • 19
  • 93
  • 159
Giuseppe Lodi Rizzini
  • 1,045
  • 11
  • 33

3 Answers3

2

You can use Object.keys to get an array of all keys and loop trough this array to console.log value correponding to each key:

var data = {
   aliquota: 23,
   imponibileLordo: 300,
   imponibileScontato: "",
   imposta: 69
};

Object.keys(data).forEach(key => console.log(data[key]));
Faly
  • 13,291
  • 2
  • 19
  • 37
0

To iterate an object use for ..in

var data = {
  aliquota: 23,
  imponibileLordo: 300,
  imponibileScontato: "",
  imposta: 69
}

for (var index in data) {
  var valueOfIndex = data[index];
  console.log(valueOfIndex);
}
brk
  • 48,835
  • 10
  • 56
  • 78
0

i've to use a numeric INDEX for cycle for cause I do other operation based on index value.

Then you'll want to reorganize your logic and code to use an array instead of an object, or at least to have your own array of property names in a defined order and loop through that. Although object properties have an order as of ES2015, it's not an order you should use as it depends on how the object was created and what the names of the properties are (e.g., do they look like array indexes or not).

You can loop through the object's properties in the ES2015+ order by using Object.getOwnPropertyNames on the object and looping through the strings in the returned array, e.g.:

Object.getOwnPropertyNames(data).forEach((name, index) => {
    // `name` will be the property name,
    // `index` will be its index in the property order
    // ...
});

...but again, it's not a good idea to rely on those being in any particular order.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875