0

The following selects all the elements from the array connections. I just want a particular one.

var route = vis.selectAll("line.route")
.data(connections)
.enter().append("svg:line")
  .attr("class", "route")
  .attr("stroke", function(d) { return '#'+routesById[d.line].colour; })
  .attr("stroke-linecap", 'round')
  .attr("x1", function(d) { return x(d.station1.longitude); })
  .attr("y1", function(d) { return y(d.station1.latitude); })
  .attr("x2", function(d) { return x(d.station2.longitude); })
  .attr("y2", function(d) { return y(d.station2.latitude); })

connections is an array(i think) and on the console is defined as

[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object…]

However I only want to select an element at a particular index. I tried to use the filter but it did not work?

.data(connections.filter(function(d) { return d[23] }))

1 Answer 1

1

use i

.data(connections.filter(function(d,i) { return i === 23; }))

https://github.com/mbostock/d3/wiki/Selections#filter

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.