Skip to content

Instantly share code, notes, and snippets.

@foobarbecue
Last active June 15, 2016 18:07
Show Gist options
  • Save foobarbecue/dc3ac848f3c1a0ad9f252db8c35b43c2 to your computer and use it in GitHub Desktop.
Save foobarbecue/dc3ac848f3c1a0ad9f252db8c35b43c2 to your computer and use it in GitHub Desktop.
Minimal case: Calling .select() changes bound data
<html>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.js">
</script>
<script>
// Setup
let body = d3.selectAll('body')
let exampleData =
[
{_id:"groupOne", items:['one','two']},
{_id:"groupTwo", items:['three','four']}
];
// Add some divs
body.selectAll('div').data(exampleData).enter().append('div');
// Add some p children of the divs
body.selectAll('div').selectAll('p').data((d)=>d.items).enter().append('p').text((d)=>d);
// Issue
console.log(body.selectAll('div').data()); // data is the same as exampleData
body.selectAll('p').select(function(){return this.parentNode}); // Select parents of all p
console.log(body.selectAll('div').data()); // Data is now ["two","four"]
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment