Why did seven consume nine? I can't understand why my index 8 is being overlooked in this d3 selection

I am currently working on a D3 script that involves a function called drawWorkingLife. This function is responsible for appending 11 images to an SVG. However, I have noticed that the script seems to skip appending the 8th image.

During my debugging process, I observed that when I log the x and y attributes being added to the images, index 8 is not being logged.

Could someone please explain why index number 8 is not appearing in the console log within the drawWorkingLife function below?

Demo:
Repo: https://github.com/radiocontrolled/sevenAteNine

function drawWorkingLife() {
 var work = svg.selectAll("image")
  .data(workingLife, function(d,i) { 
    return d[i]; 
  })
  .enter()
  .append("g");

  work
  .append("svg:image")
  .attr(opts)
  .attr({
    "x" : function(d,i) {
      console.log(i);
      // what causes index 8 to be skipped?

    },
    "y" : function(d,i) {
      console.log(i);
      // what causes index 8 to be skipped? 
    },       
    "class" : function(d,i){
      return d;
    }
  })
  .transition()
  .duration(1000)
  .style("opacity", 1);

}

Answer №1

The issue lies within the section of code that links the information with the chosen item:

.data(workingLife, function(d,i) { 
  return d[i]; 
})

The function used as the second parameter instructs D3 on how to distinguish each data point uniquely. By returning the ith character of the data, you are not providing a unique identifier in this case. Since all your strings are "workingLife" and the character "i" appears multiple times in the string, D3 interprets data values 4 and 8 as identical. Thus, the 8th value is perceived as a duplicate by D3.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error E11000 occurred while attempting to insert a document into a MongoDB collection due to a duplicate key

While conducting testing, I populated my database with 15000 instructor data. For each instructor, I needed to insert 100 courses. To achieve this, I utilized a for loop to retrieve all instructor IDs and then store 100 courses for each instructor ID. Howe ...

What is the best way to add an element while preserving its original placement?

I need a solution to duplicate the code of an element and transfer it to another element without removing it from its original position. Currently, I have a JavaScript function that allows you to move a picture to a box when it's clicked. However, th ...

Struggling to retrieve external JSON data with JSONP

My goal is to fetch a remote JSON file using jQuery 1.11.1. The server does support jsonp, and I am able to download the .jsonp file by simply entering the call address with "?callback=foo" in a browser. However, when attempting to retrieve it via ajax, i ...

The function getPlayerState() does not exist in YT

Trying to access YouTube states using JavaScript in order to manage a video slideshow, but encountering an error message indicating that getPlayerState() is not a function. While I can directly access the playerState property, I prefer to follow the correc ...

The HTML5 postMessage function often returns an undefined value

My attempt to establish cross-domain communication using postMessage between a page hosted on example1.com and an iframe within the same page on example2.com is facing some challenges. Once the iframe finishes loading, it sends a postMessage to the parent ...

Having trouble getting my initial Vue.js code to function properly alongside basic HTML

I am in need of assistance with this code as it does not seem to be working properly. I am using an external js file named app.js for the Vue JS code in the index.html file. Here is the code from index.html: new vue({ el: '#app', data: { ...

Can you provide an update on the progress of the JavaScript target development in antlr3?

What is the current status of Antlr3's Javascript target? I attempted to generate a parser for a simple grammar, but encountered numerous compiler errors in the generated code. After investigating the website, I downloaded the latest antlr3.5-snapshot ...

Using numerous columns in a WHERE statement

I am currently working on creating a URL query string that allows users to enter a parameter and search my MySQL database for a random quote from an IRC channel. The quotes are stored in the database, and I want the search to be conducted in both the "titl ...

Updating JSON data post XMLHttpRequest call

Currently experiencing a puzzling moment. I'm attempting to insert more information into my object after retrieving it from an external source (just for testing purposes, I intend to add random values). Without further ado: As an example, here is w ...

Having trouble showing images from block content using PortableText?

It's been quite a while now that I've been stuck on this issue. Despite being in a learning phase, I find myself unable to progress due to this specific problem. While links and text elements are functioning properly, I have encountered difficul ...

implementing ajax form submission in codeigniter

After submitting my form, validation is checked in the JavaScript file, and then the kickerLogin() function is called. I receive an alert message of datastring. However, the data is not sent to the specified URL in the AJAX request, but the form still ge ...

Using the $inc operator in mongoose to avoid decrementing a value below zero

My code successfully deducts credit from a user using $inc in Mongoose, but the concern is that the value can become negative, which is not ideal. Is there any way to prevent this? module.exports.deduct_credit = function(subscriber_email,callback){ Us ...

Managing HTTP Requests on a Website Using HTTPS

On my html page, I have references to Java script files hosted by Google using Http. However, since my site is Https, the page loads with a message saying "Only secured content is displayed." I need to change these calls to use Https instead of http. I at ...

The Ajax success callback is failing to update the background-image property after receiving a successful JSON response

I am struggling with setting a background image in wordpress despite receiving a successful ajax response. The image url is being returned successfully in the json response, but I can't seem to set it as a background image. Here is the ajax function ...

I am looking for an alternative approach to retrieve the data from the controller in case of success instead of the method provided in the code below

In this code snippet, I am looking for an alternative way to retrieve data from the controller upon successful execution, rather than checking it as data.msg. I want to avoid using strings or boolean values like SuccessWithPass. Controller snippet: if ...

Press on the button that is currently in your field of view

I have a web page with multiple buttons inside div elements. I am looking to automate the process of clicking the "Buy" button that is currently visible on the screen when the user presses the B key. $(document).keydown(function(e) { if (e.keyCode == ...

What is the best way to fill in fields on my webpage using a dropdown menu choice?

I'm exploring the world of ASP.NET MVC, AJAX, and jQuery for the first time. I'm attempting to populate some text boxes on my website based on a dropdown selection but it seems like the data isn't getting through. I suspect that the 'ch ...

What could be causing React to throw an invalid hook error when using useRoutes?

I encountered an error while attempting to add a new route to my project. import React from "react"; import News from "./NewsComponents/News"; import Photos from "./PhotosComponents/Photos"; import Contact from "./Contact"; import Home from "./Home"; ...

Is there a way to add additional content below each row within my table?

I've been searching extensively online, but I haven't found a solution tailored to my specific situation. I have a Bootstrap table and I'm trying to implement a feature where clicking on a row will display additional information related to t ...

There are no errors thrown when assigning a numeric value within an object literal

Here is a piece of code to consider: let logged = 1; let x = {logged} //{logged: 1} x['logged']; // 1 x['logged']['index']; //undefined x['logged']['index'] = 0; x; // {logged: 1} x['logged'] ...