D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
Abhay-Joshi-Git
Full window
Github gist
existing elements with key function
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v4.min.js"></script> <style> body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } </style> </head> <body> <div class="container"> <div>Billy</div> </div> <script> var scores = [ {name: 'Alice', scrore: 96}, {name: 'Billy', scrore: 92}, {name: 'Cindy', scrore: 86}, {name: 'David', scrore: 76}, {name: 'Emily', scrore: 94}, ]; d3.select('.container') .selectAll('div') // here we are passing key function // if key matches we don't get data if that is not already set // instead we access the element by this .data(scores, function(d) { console.log('key function', d, this); return d? d.name : this.innerText; }) .enter() .append('div') .text(function(d) {return d.name}) .style('color', 'red'); </script> </body>
https://d3js.org/d3.v4.min.js