Browse JavaScript object

2
<div id="container" style="height: 400px"></div>


<script>

var chart = Highcharts.chart('container', {

                title: {
                    text: ''
                },
                xAxis: {
                    //categories: fecha,
                    labels: {
                        rotation: -80,
                        style:
                            {
                                fontFamily: 'Sans-serif',
                                fontSize: '12px',
                            }
                    },
                },
                credits:
                {
                    enabled: false,
                },
                plotOptions: {
                    line: {
                        dataLabels: {
                            enabled: true,
                            style: {
                                fontWeight: 'normal',
                                color: '#000000',
                                fontSize: '10px',
                                fontFamily: 'Sans-serif'
                            },
                        },
                        enableMouseTracking: false
                    },
                },
series: [{
        name: 'Installation',
        data: [43934, 52503, 57177]
    }, {
        name: 'Manufacturing',
        data: [24916, 24064, 29742]
    }, {
        name: 'Sales & Distribution',
        data: [11744, 17722, 16005]
    }, {
        name: 'Project Development',
        data: [null, null, 7988]
    }, {
        name: 'Other',
        data: [12908, 5948, 8105]
    }],
            });

for(let i  = 0; i< chart.series.length; i++){

    for(let j  = 0; j< chart.series[i].data.length; j++){

        //alert(chart.series[i].data[j]);
        //console.log(series[i].data[j]);

    }  

}

</script>
    
asked by Pollo 22.08.2018 в 19:06
source

2 answers

1

With the update you did, you had to do:

chart.series.forEach(o => {
    o.data.forEach(e => {
      console.log(e);
    });
  });

or just to show the numbers but not go through them

chart.series.forEach(o => {
    console.log(o.data.toString())
  });

This is a possible way shorter than the for:

var series = [
  {
    name: "Installation",
    data: [43934, 52503, 57177]
  },
  {
    name: "Manufacturing",
    data: [24916, 24064, 29742]
  },
  {
    name: "Sales & Distribution",
    data: [11744, 17722, 16005]
  },
  {
    name: "Project Development",
    data: [null, null, 7988]
  },
  {
    name: "Other",
    data: [12908, 5948, 8105]
  }
];

series.forEach(o => {
  o.data.forEach(e => {
    console.log(e);
  });
});

In this case we always iterate over arrays, to the object we access directly. Using for serious:

var series = [
  {
    name: "Installation",
    data: [43934, 52503, 57177]
  },
  {
    name: "Manufacturing",
    data: [24916, 24064, 29742]
  },
  {
    name: "Sales & Distribution",
    data: [11744, 17722, 16005]
  },
  {
    name: "Project Development",
    data: [null, null, 7988]
  },
  {
    name: "Other",
    data: [12908, 5948, 8105]
  }
];

for(let i  = 0; i<series.length; i++){

    for(let j  = 0; j<series[i].data.length; j++){
      
        console.log(series[i].data[j]);
    
    }  

}

The form you use is the same except for the chart, which I do not know if it is a property that does not exist or is part of a larger object.

    
answered by 22.08.2018 / 19:24
source
1

You can help the forEach loop, by going through the variable that contains your object; then from the console.log access the name of the key you want to print; so

let series = [

      {
        "name": "Installation",
        "data": [43934, 52503, 57177]
      },
      {
         "name": "Installation 2",
        "data": [43934, 52503, 57177]
      }]


series.forEach(value => {
  console.log(value["name"])
})
  

As you can see, the var value contains the complete data of the object   that is traveled; then you can from the syntax of brackets and between   quotes place the name of the key you want to print; in this   case for the example I put name, but it can be the one you want

    
answered by 22.08.2018 в 19:42