Javascript 合并共享公共键或值的对象
我正在加载一个大的CSV以在javascript中使用——从中创建SVG元素。 变量中有一个名为code的属性。在CSV中,有一列名为“物种”。“代码”和“种类”包含相同的字母代码。 我希望能够将CSV中的数据与var树进行比较,如果CSV中的特定记录的“物种”字段与变量中的“代码”值相同,我希望从变量中返回“公共”值。 CSV文件有150000条记录,这就是为什么我不想添加另一个具有公共名称的列 这是变量的一部分(总共有54个对象): 以下是data.csv的一部分(此处共有150000条记录): 以下是我尝试过的,但不起作用:Javascript 合并共享公共键或值的对象,javascript,d3.js,underscore.js,Javascript,D3.js,Underscore.js,我正在加载一个大的CSV以在javascript中使用——从中创建SVG元素。 变量中有一个名为code的属性。在CSV中,有一列名为“物种”。“代码”和“种类”包含相同的字母代码。 我希望能够将CSV中的数据与var树进行比较,如果CSV中的特定记录的“物种”字段与变量中的“代码”值相同,我希望从变量中返回“公共”值。 CSV文件有150000条记录,这就是为什么我不想添加另一个具有公共名称的列 这是变量的一部分(总共有54个对象): 以下是data.csv的一部分(此处共有150000条记录
var treeCode = trees.forEach(function(each) { return each.code; }); // returns the value of "code" for each object in the trees variable
var treeCommon = trees.forEach(function(each) { return each.common; }); // returns the value of "color" for each object in the trees variable
var tip = d3.tip() // d3.tip is a tooltip library for D3.js
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d){if (d.SPECIES == treeCode){ // the data from the csv was loaded in earlier, and d.SPECIES returns the "SPECIES" field
return treeCommon}
})
任何关于需要做什么的想法都将不胜感激!如果我能澄清什么,请告诉我。
完整代码如下:
您可以在该代码中看到一个带有十六进制颜色的looong if/else语句。这是我想使用这种方法的另一个例子。
谢谢你的关注 有一些关于下划线的很棒的文档。D3是一个相当健壮的库,所以我打赌其中一些函数也已经内置到D3中了 因此,为了澄清这一点,您需要遍历CSV中的记录,并从trees对象中提取相应的数据。下面是我将如何使用下划线来实现这一点:
// Information about each tree type
var treeInfo = [
{
code: 'ACPL',
common: 'Norway Maple',
genus: 'Acer',
species: 'platanoides',
family: 'Sapindaceae',
color: '#00567e'
},
...
];
// imported from CSV
var treeList = [
{
DIAMETER: 15,
SPECIES: "ACPL",
longitude: -73.935944,
latitude: 40.668076
}
...
]
// loop through the imported list of trees
_.each(treeList, function(){
// _.each() makes `this`` refer to the object in the list as it loops through
// find the info for this tree species
var info = _.findWhere(treeData, {"code": this.SPECIES});
// insert the keys and values from the info object into the original object from the treeList
_.extend(this, info);
})
//now treeList contains all the corresponding info (by species) from treeInfo for each tree
2件事,你是否将CSV解析为JS对象?你能用下划线吗?下划线有一些很好的函数,可以在最少的代码中获得结果,如果没有,您需要编写一个循环来遍历每个记录并比较(下划线在引擎盖下所做的)Hi@PatrickGunderson。1.是的,D3——我在CSV中加载的内容——提供了解析逗号分隔值的内置支持。(API参考号:)2。我从来没有用过下划线,但当然,我对它持开放态度。谢谢非常感谢您抽出时间写下这篇文章!这非常有帮助。基于您的下划线示例,我已经大致了解了如何使用D3实现这一点。再次感谢。
var treeCode = trees.forEach(function(each) { return each.code; }); // returns the value of "code" for each object in the trees variable
var treeCommon = trees.forEach(function(each) { return each.common; }); // returns the value of "color" for each object in the trees variable
var tip = d3.tip() // d3.tip is a tooltip library for D3.js
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d){if (d.SPECIES == treeCode){ // the data from the csv was loaded in earlier, and d.SPECIES returns the "SPECIES" field
return treeCommon}
})
// Information about each tree type
var treeInfo = [
{
code: 'ACPL',
common: 'Norway Maple',
genus: 'Acer',
species: 'platanoides',
family: 'Sapindaceae',
color: '#00567e'
},
...
];
// imported from CSV
var treeList = [
{
DIAMETER: 15,
SPECIES: "ACPL",
longitude: -73.935944,
latitude: 40.668076
}
...
]
// loop through the imported list of trees
_.each(treeList, function(){
// _.each() makes `this`` refer to the object in the list as it loops through
// find the info for this tree species
var info = _.findWhere(treeData, {"code": this.SPECIES});
// insert the keys and values from the info object into the original object from the treeList
_.extend(this, info);
})
//now treeList contains all the corresponding info (by species) from treeInfo for each tree