简体   繁体   中英

How to display the values from this array in the console of javascript?

How can I display the values of the given array in javascript? In other words, how can use console.log over "pie" to display (42.9, 37.9 and 19.2)?

It tried console.log(Object.values(pie)) but it didn't work. Thanks a lot.

在此处输入图片说明

This is how I created the array:


var width = 350
    height = 350
    margin = 40

// The radius of the pieplot is half the width or half the height (smallest one). I subtract a bit of margin.
var radius = Math.min(width, height) / 2 - margin


// append the svg object to the div called 'my_dataviz'
var svg = d3.select("#my_dataviz_b")
  .append("svg")
    .attr("width", width)
    .attr("height", height)
  .append("g")
    .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

    var color =["#98abc5", "#8a89a6", "#7b6888"]
    var annotations = ["Home win", "Draw game", "Away win"]
  

  
  var data = d3.selectAll('.values_half_before').nodes();


  

  var pie = d3.pie()   //we create this variable, for the values to be readeable in the console
  .value(function(d) {return d.innerHTML; })(data);

If you are looking to log individual values of your array you could loop over them with a for loop.

for (let i = 0; i < pie.length; i++) {
    console.log(pie[i].value);
}

You could also use console.table . This will display the values in a nice table overview.

console.table(pie);

You can do it this way:

pie.forEach((item) => {
  console.log(item.value)
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM