existing elements with key function
This example demonstrates how D3’s data join behaves when a key function is supplied, using a hardcoded array of five student score objects. It shows that when existing DOM elements match data by key (here, the name property), the join does not re-bind data to those matching elements, but instead leaves them untouched. The code selects all `div` elements inside a container, passes the scores array with a key function that logs both the data and the current element to the console, and then appends new `div` elements for unmatched data. The newly appended divs display each student’s name in red, while the pre-existing “Billy” div remains unstyled, illustrating how the key function controls element reuse in D3 v4’s `enter` selection.
AI-generated descriptionBuilt with blockbuilder.org