合并共享公共键或值的对象

Merge objects that share a common key or value

我正在加载一个大的 CSV 以在 javascript 中使用 -- 从中创建 SVG 元素。 在名为code的变量中有一个属性。在 CSV 中,有一个名为 SPECIES 的列。 "Code" 和 "SPECIES" 包含相同的字母代码。 我希望能够将 CSV 中的数据与 var 树进行比较,如果对于 CSV 中的特定记录,"SPECIES" 字段与变量中的 "code" 值相同,我想 return 来自变量的 "common" 值。 CSV 文件有 150,000 条记录,这就是为什么我不想向它添加另一个具有通用名称的列。

这里是部分变量(一共有54个对象):

var trees = [
        { code: 'ACPL', 
          common: 'Norway Maple', 
          genus: 'Acer', 
          species: 'platanoides', 
          family: 'Sapindaceae', 
          color: '#00567e'
        },
        { code: 'ACRU', 
          common: 'Red Maple', 
          genus: 'Acer', 
          species: 'rubrum', 
          family: 'Sapindaceae', 
          color: '#0084ff'
        },
        { code: 'ACSA1', 
          common: 'Silver Maple', 
          genus: 'Acer', 
          species: 'saccharinum', 
          family: 'Sapindaceae', 
          color: '#c3c3c3'
        }
];

这是 data.csv 的一部分(这里总共有 150,000 条记录):

DIAMETER,SPECIES,longitude,latitude
15,ACPL,-73.935944,40.668076
22,ACPL,-73.934644,40.667189
28,ACSA1,-73.941676,40.667593
27,ACPL,-73.935095,40.666322
9,ACRU,-73.941297,40.667574
27,ACPL,-73.935149,40.666324
19,ACRU,-73.933664,40.666244

这是我试过的方法,但没有用:

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}
        })

任何关于需要做什么的想法都将不胜感激!如果我能澄清任何事情,请告诉我。 完整代码在这里:https://github.com/jhubley/street-trees/blob/master/index.html 您可以在该代码中看到带有十六进制颜色的 looong if/else 语句。这是我想使用这种方法的另一个例子。 感谢您的观看!

http://underscorejs.org/ 上有一些关于下划线的很棒的文档。 D3 是一个相当强大的库,所以我敢打赌其中一些函数也已经内置到 D3 中。

所以澄清一下,您想 运行 通过 CSV 中的记录并从树对象中提取相应的数据。这是我如何使用下划线来做到这一点:

// 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