【问题标题】:Identifying a null vector with array created with d3.js使用 d3.js 创建的数组识别空向量
【发布时间】:2021-02-20 15:56:07
【问题描述】:

我怎样才能显示 if - else 语句,这样它才能真正起作用?提前谢谢

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

console.log('pie', pie)

if (pie = ['0', '0', '0']) {
  console.log('it is a null vector');
} else {
  console.log('it is not a null vector');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<div class="values_half_before">1</div>
<div class="values_half_before">0</div>
<div class="values_half_before">0</div>

重要提示:我希望脚本通过更改 if - else 语句而不是上部来工作。

非常感谢

【问题讨论】:

标签: javascript arrays d3.js


【解决方案1】:

单个= 符号是一个赋值。您在 if 语句中覆盖 pie['0', '0', '0'] evaluates to true,因为它不是 数组。

我建议您更早地评估 if 语句。考虑以下情况,您可以在其中更改 div 内的值以查看输出更改:

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

if(data.every(function(d) { return d.innerHTML === '0'; })) {
  console.log('it is a null vector');
} else {
  console.log('it is not a null vector');
  var pie = d3.pie() //we create this variable, for the values to be readeable in the console
    .value(function(d) {
      return d.innerHTML;
    })(data);

  console.log('pie', pie)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<div class="values_half_before">0</div>
<div class="values_half_before">0</div>
<div class="values_half_before">0</div>

【讨论】:

    猜你喜欢
    • 2020-07-27
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    相关资源
    最近更新 更多