在d3中检索数组的特定索引选择

时间:2016-02-01 19:37:05

标签: javascript d3.js

以下选择数组连接中的所有元素。我只想要一个特定的人。

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是一个数组(我认为),在控制台上定义为

[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…]

但是我只想在特定索引处选择一个元素。 我尝试使用过滤器,但它不起作用?

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

1 个答案:

答案 0 :(得分:1)

使用我

<?php

$string = 'Armin van Buuren - Rain ft. Cathy Burton (Urbanstep Remix).mp3
Alpha Drop - Spring Fever.mp3
Beatcore - Tonight ft. Lynn Boyer.mp3
iru1919 - 天狐.mp3';

$regex = '~              # delimiter
        ^                # anchors regex to the beginning
        (?<artist>[^-]+) # capture everything but a dash to group "artist"
        -          
        (?<rest>.*)      # capture everything but .mp3 to group "rest"
        (?:\.mp3)
        $
        ~xm';            # multiline and freespace mode
preg_match_all($regex, $string, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
    $artist = trim($match["artist"]);
    list($title, $artist2) = preg_split("~ft\.~", $match["rest"]);
    echo "Artist: " . trim($artist) . 
        ", Title: " . trim($title) . 
        (!empty($artist2)?", Second Artist: $artist2":"") . 
        "\n";
}
// output:
// Artist: Armin van Buuren, Title: Rain, Second Artist:  Cathy Burton (Urbanstep Remix)
// Artist: Alpha Drop, Title: Spring Fever
// Artist: Beatcore, Title: Tonight, Second Artist:  Lynn Boyer
// Artist: iru1919, Title: 天狐
?>

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