Search code examples
javascriptd3.jsjslint

How can I do d3 without this to support JSLint


How to convert below code as valid JSLint code i.e. (dont use this keyword) https://codepen.io/anon/pen/eGGXKR

d3.selectAll(".test").style("background-color", function(){
  return d3.select(this).html();
})
.test{
  padding:4px;
  margin:4px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.9.1/d3.min.js"></script>
<div class="test">red</div>
<div class="test">green</div>
<div class="test">blue</div>
<div class="test">pink</div>
<div class="test">yellow</div>


Solution

  • In a D3 method, like style(), this is the same of the third and second arguments combined.

    So, this...

    d3.selectAll(".test").style("background-color", function(){
        return d3.select(this).html();
    })
    

    .... is the same of:

    d3.selectAll(".test").style("background-color", function(d,i,n){
        return d3.select(n[i]).html();
    })
    

    Here is your code with that change:

    d3.selectAll(".test").style("background-color", function(d,i,n){
      return d3.select(n[i]).html();
    })
    .test{
      padding:4px;
      margin:4px;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.9.1/d3.min.js"></script>
    <div class="test">red</div>
    <div class="test">green</div>
    <div class="test">blue</div>
    <div class="test">pink</div>
    <div class="test">yellow</div>