0

how could I display the if - else sentence, so that it would really work? thanks in advance

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>

enter image description here

Important: I would like to the script to work by changing the if - else statement, not the upper part.

Thanks a lot

Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49
Miguel Gonzalez
  • 398
  • 1
  • 12
  • I guess the essential question is - "[How to compare arrays in JavaScript?](https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript)" – Mark McClure Nov 08 '20 at 16:07

1 Answers1

0

A single = sign is an assignment. You overwrite pie inside the if statement, and ['0', '0', '0'] evaluates to true, because it's not an empty array.

I recommend that you evaluate the if statement earlier. Consider the following, where you can change the values inside the divs to see the output change:

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>
Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49