javascript - d3.js: efficient filtering of large selection -
given large selection (several tens of thousands) want filter 1 or more element matching given ids.
for them moment, use that:
var ids = [1, 42, …, 13]; var idsobj = arraytoobject(ids); // { 1: 1, 42: 1, …, 13: 1 } var filteredselection = theselection.filter(function(d) { return idsobj[d.id]; });
what not solution runtime not depend on amount of ids. iterates on whole selection regardless of whether ids have been found already.
is there way manually iterate on selection? i've theselection.each()
, seems there no way break iteration. or know approach?
edit: maybe can work that:
for (var = 0; < theselection[0].length; i++) { // d3.select(theselection[0][i]).datum().id };
ok, think i've got solution. please comment, if there better way ;)
var filterednodes = []; (var = 0; < theselection[0].length; i++) { var el = theselection[0][i]; var d = d3.select(el).datum(); if (idsobj[d.id]) { filterednodes.push(el); if (filterednodes.length == ids.length) { break; } } } var filteredselection = d3.selectall(filterednodes);
by way, possible that:
var filteredselection = d3.select(); (…) { … filteredselection.add(el); … }
Comments
Post a Comment