Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

D3 data binding matches an array of data to the DOM elements in a selection. Each datum either updates an existing element, enters without an element, or leaves an element without matching data. Use .join() for the usual create-update-remove pattern; add a key function when records should keep their visual identity after reordering.

What data binding does

Think of a data join as comparing two collections: the elements already selected and the data supplied now. D3 pairs them and makes the unmatched cases available as enter and exit selections. These names describe the result of one join, not permanent categories of DOM nodes.

Calling .data(data) associates values with selected elements and returns the update selection. D3 stores each bound value on its element in __data__, making it available if you select that element again; the D3 selection.data reference calls this data “sticky.” The binding defines which data and elements correspond; it does not, by itself, create missing elements.

Make a basic join with .join()

Start with an SVG selection and an array such as const data = [{name: "A", value: 12}, {name: "B", value: 20}];. A concise join looks like this:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
svg.selectAll("circle")
  .data(data)
  .join("circle")
  .attr("r", d => d.value)
  .attr("cx", d => x(d.name))
  .attr("cy", d => y(d.value));

Here, selectAll("circle") selects the current circles in the SVG. .data(data) compares them with the new array, and .join("circle") appends a circle for each entering datum, keeps the updating circles, and removes exiting ones. The selection returned by .join() combines entering and updating elements, so the attribute setters that follow apply to both.

#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

Change the array length and run the join again: extra data produce new circles, while extra old circles are removed. Keep the same number of records but change their values, and the attribute setters update the existing marks.

When enter, update and exit need different treatment

Most joins need no separate callbacks. Use them when entering, updating and exiting elements need distinct setup or behavior:

Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.
svg.selectAll("circle")
  .data(data, d => d.id)
  .join(
    enter => enter.append("circle").attr("r", 0),
    update => update,
    exit => exit.remove()
  )
  .attr("r", d => radius(d.value));

The enter callback creates circles and gives them an initial radius; the update callback keeps existing circles; the exit callback removes unmatched circles. The final radius setter applies to the merged enter-and-update selection. The D3 joining reference also supports transitions in these callbacks: if an enter or update callback returns a transition, D3 merges the underlying selections.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In the older explicit pattern, call .enter() to handle missing elements and .exit() to handle elements without data. If an operation should apply to both newly entered and updated elements, merge those selections with .merge(update). Otherwise it is easy to configure only new elements and leave existing ones stale.

Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.

Choose index matching or a key

Without a key function, D3 pairs data and elements by position: the first datum with the first element, the second datum with the second element, and so on. This is appropriate when order stays stable and position itself carries the intended identity.

If records can move, be sorted, or be refreshed as newly created JavaScript objects, use a stable identifier so an element continues to represent the same record. The key function is evaluated for existing elements and incoming data; its returned key is a string identifier. For records with unique IDs, for example, use .data(data, d => d.id).

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
Matching approach How D3 matches Best fit
Index join By position in the selection and data array Order is stable, or position is the intended identity
Key join By the string identifier returned by the key function Records may reorder or be reconstructed but should retain their element identity

Make keys unique within the relevant selection group. D3 sends duplicate keys on existing elements to exit and duplicate keys in incoming data to enter, rather than matching those duplicates as one-to-one identities. A stable key also matters when refreshed data contains new object instances: matching by a field such as an ID can connect records across arrays even though the JavaScript objects themselves differ. See the Square Intro to D3 data-binding tutorial for a beginner-friendly illustration.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Bind data in nested selections

D3 performs joins independently within each selection group. If there is one group, pass its array directly to .data(). When each parent has its own child data, pass a function that returns the array for each group, often from the parent datum.

Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.

For example, after binding rows to table-row elements, bind each row’s values to its cells with .data(d => d). In a grouped SVG where each parent datum has a children array, the analogous pattern is .data(d => d.children). This is why a flat array is not a substitute when every parent needs different child data. The D3 data-joining documentation demonstrates the row-and-cell pattern.

Common mistakes to avoid

  • Expecting .data() to create elements: it defines the join. Use .join() or handle .enter() to create missing nodes.
  • Updating only entering elements: shared attributes should be applied to the merged selection returned by .join(), or to enter and update selections merged explicitly in the older pattern.
  • Ignoring exiting elements: .join() removes them by default; supply an exit callback when they need different handling.
  • Using position as identity after a reorder: use a unique, stable key when a visual element should follow a particular record.
  • Passing one flat array to every nested group: use a data function when child data depends on each parent datum.
  • Reusing duplicate keys: duplicate element keys exit and duplicate incoming-data keys enter, so they do not provide dependable identity.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.