Help with Javascript for loop

2

I would like to know how to make a for loop to dynamically fill parameters in the following code:

              data: [{
                        type: "pie",
                        startAngle: 25,
                        toolTipContent: "<b>{label}</b>: {y}%",
                        showInLegend: "true",
                        legendText: "{label}",
                        indexLabelFontSize: 16,
                        indexLabel: "{label} - {y}%",
                        dataPoints: [{
                            y: parseFloat(result.valor[0]),
                            label: result.nombres[0]
                        }, {
                            y: parseFloat(result.valor[1]),
                            label: result.nombres[1]
                        }, {
                            y: parseFloat(result.valor[2]),
                            label: result.nombres[2]
                        }, {
                            y: parseFloat(result.valor[3]),
                            label: result.nombres[3]
                        }, {
                            y: parseFloat(result.valor[4]),
                            label: result.nombres[4]
                        }, {
                            y: parseFloat(result.valor[5]),
                            label: result.nombres[5]
                        }, {
                            y: parseFloat(result.valor[6]),
                            label: result.nombres[6]
                        }, {
                            y: parseFloat(result.valor[7]),
                            label: result.nombres[7]
                        }]

                    }]
                });

The idea is that y: parseFloat (result.valor [i]),                 label: result.nombres [i];

Generated automatically without having to enter the parameters

    
asked by nostradonut 03.11.2018 в 14:49
source

1 answer

1

If the idea would be to add the dataPoints % of% values that are in n to the result.valor fix, then you could do the following:

var dataPoints = [];
// Para cada elemento en el arreglo
result.valor.forEach(function(valor, posicion) {
  // Lo agregamos
  dataPoints.push({
    y: parseFloat(valor),
    label: result.nombres[posicion]
  });
});

var data = [{
  type: "pie",
  startAngle: 25,
  toolTipContent: "<b>{label}</b>: {y}%",
  showInLegend: "true",
  legendText: "{label}",
  indexLabelFontSize: 16,
  indexLabel: "{label} - {y}%",
  dataPoints: dataPoints //ACA pasamos el arreglo
}];
    
answered by 03.11.2018 / 15:06
source