从已解析的CSV数组中删除重复项

时间:2019-01-12 18:01:29

标签: javascript arrays d3.js

我在上一个问题的基础上取得了进展,但遇到了最后一个障碍。我在csv中有以下数据。

this_year   |   minus_one_year  |   minus_two_year  |   minus_three_year
-------------------------------------------------------------------------
1           |   2               |   2               |   3
-------------------------------------------------------------------------
4           |   5               |   5               |   5
-------------------------------------------------------------------------
2           |   2               |   2               |   2
-------------------------------------------------------------------------
4           |   5               |   4               |   4
-------------------------------------------------------------------------
1           |   2               |   3               |   3
-------------------------------------------------------------------------

我阅读了这个csv文件,现在我需要产生节点。节点包含一个节点(列标题)以及该值。因此,在以上数据中,您可以看到this_year具有3个不同的 值1、2和4,因此用于此的节点应如下所示。

{
  "nodes": [
    {
      "name": "1",
      "node": "this_year"
    },
    {
      "name": "2",
      "node": "this_year"
    },
    {
      "name": "4",
      "node": "this_year"
    }
  ]
}

还应生成其他列的其他节点。到目前为止,我有这个

d3.csv('my_csv.csv')
    .then(function(data) {

        let graph = {"nodes" : [], "links" : []};

        graph.nodes = data.reduce(function(acc, line){
            return acc.concat(Object.entries(line).map(function(column){
                return {name: column[0], node: column[1]}
            }))}, [])
            .sort(function (n1, n2) {
                return d3.ascending(n1.name, n2.name);
            });

        console.log('nodes:', JSON.stringify(graph.nodes));

    }).catch(function(error){
});

这将产生以下内容

[
  {
    "name": "this_year",
    "node": "1"
  },
  {
    "name": "this_year",
    "node": "4"
  },
  {
    "name": "this_year",
    "node": "2"
  },
  {
    "name": "this_year",
    "node": "4"
  },
  {
    "name": "this_year",
    "node": "1"
  }
]

因此,它具有正确的格式,但它正在输出重复项,对于1、2和4,每个应只包含一个。如何删除这些重复项?我看过reduceRight, 这是我可以使用的东西吗?

谢谢

1 个答案:

答案 0 :(得分:1)

在假设您的数据整理得很好的前提下,一种快速而肮脏的方法是将对象的值组合为字符串,并根据它们生成SetSet本质上不能重复。但是同时,JavaScript Set只能理解原始数据类型(数字,字符串等)的重复项。

合并值,将其转换为Set,然后将其转换回以前的数据结构。

请告诉我您是否对我的语法感到困惑。我自由地使用了现代JavaScript语法。

编辑:您可以使用Array#filter来选择数组中应该存在的内容。

const values = [
    {
        "name": "this_year",
        "node": "NA"
    },
    {
        "name": "this_year",
        "node": "1"
    },
    {
        "name": "this_year",
        "node": "4"
    },
    {
        "name": "this_year",
        "node": "2"
    },
    {
        "name": "this_year",
        "node": "4"
    },
    {
        "name": "this_year",
        "node": "1"
    }
]
function constructUniques(array) {
    const concattedStringValues = array
           .filter(({node}) => node !== 'NA')
           .map(({name, node}) => `${name} ${node}`)
    const uniqueStrings = new Set(concattedStringValues)
    return [...uniqueStrings].map(concattedVal => {
        const [name, node] = concattedVal.split(' ')
        return {name, node }
    })
}

console.log(constructUniques(values))