Json d3过滤器内部循环的适当方式(或者,如何选择多个功能)
假设我有一个这样的数组,我想用它进行模式匹配:Json d3过滤器内部循环的适当方式(或者,如何选择多个功能),json,loops,d3.js,topojson,Json,Loops,D3.js,Topojson,假设我有一个这样的数组,我想用它进行模式匹配: var mich = ["Michigan", "Connecticut", "Florida", "New York"]; var arrayLength = mich.length; 我有这样一个topojson对象,嵌套在访问topojson文件的基本d3.json函数中: var allstates = topojson.feature(us, us.objects.layer1); 我使用以下方法对其进行过滤: var fromsta
var mich = ["Michigan", "Connecticut", "Florida", "New York"];
var arrayLength = mich.length;
我有这样一个topojson对象,嵌套在访问topojson文件的基本d3.json函数中:
var allstates = topojson.feature(us, us.objects.layer1);
我使用以下方法对其进行过滤:
var fromstate = allstates.features.filter()[0];
如何在topojson.features中找到与我的数组匹配的所有对象?通过过滤器内数组的循环仅与第一个对象匹配。也就是说,这在过滤器内部失效:
function (d){ for (i=0; i<arrayLength; i++){ return d.properties.NAME == mich[i];}
function(d){for(i=0;i在函数中尝试这个
return mich.indexOf(d.properties.NAME) >= 0;
而不是
for (i=0; i<arrayLength; i++){ return d.properties.NAME == mich[i];}
用于(i=0;i您想要所有具有该名称的对象?@Mritunjay我想要所有与mich数组中的字符串匹配的对象。您能发布完整的代码吗,我想这只是刚刚提供给筛选的函数。@Mritunjay您想要的是哪种代码o?json文件等等?从哪里来的d
将在数组中调用>可能吧。这段代码,所以我可以复制你的问题。这是有效的,也不是。问题是它返回一个对象数组,每个对象都可以作为d3的数据对象。因此,你需要在这些对象上进行另一个循环来显示每个对象。@Incodeveritas这是另一个问题,但我回答了你在问题中提出的问题,问题是什么是的,它在过滤后只显示了一个项目。是的!但是对于这个问题,你会怎么做?如果你只是想显示所有的项目,循环和显示,那有什么问题。我对d3不太熟悉。对不起。这是一个javascript问题。d3架构不是这样的
var mich = ["Michigan", "Connecticut", "Florida", "New York"];
var allstates = topojson.feature(us, us.objects.layer1);
var fromstate = allstates.features.filter( function (d){
for (i=0; i<mich.length; i++){
if (d.properties.NAME == mich[i]) {
return true;
}
//otherwise the loop continues
}
//if none of them matched
return false;
});
var fromstate = allstates.features.filter( function (d){
return mich.indexOf(d.properties.NAME) != -1;
});