0

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.

enter image description here

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);
2
  • 1
    Please provide JS code you used. It's really difficult to guess how to access those variables. Commented Nov 8, 2020 at 18:34
  • console.log(pie.map(x => x.value)) Commented Nov 8, 2020 at 18:58

2 Answers 2

1

You can do it this way:

pie.forEach((item) => {
  console.log(item.value)
});
Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.