为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 javascript 控制台中显示该数组中的值?

如何在 javascript 控制台中显示该数组中的值?

慕丝7291255 2023-07-20 15:06:38
如何在 JavaScript 中显示给定数组的值?换句话说,如何使用console.log over“pie”来显示(42.9、37.9和19.2)?它尝试了 console.log(Object.values(pie)) 但没有成功。多谢。这就是我创建数组的方式: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 回答

?
慕桂英3389331

TA贡献2036条经验 获得超8个赞

你可以这样做:

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


查看完整回答
反对 回复 2023-07-20
?
慕尼黑5688855

TA贡献1848条经验 获得超2个赞

如果您希望记录数组的各个值,您可以使用 for 循环遍历它们。

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

您也可以使用console.table. 这将在一个漂亮的表格概览中显示这些值。

console.table(pie);


查看完整回答
反对 回复 2023-07-20
  • 2 回答
  • 0 关注
  • 126 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信