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

标签 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, 这是我可以使用的东西吗?

谢谢

最佳答案

假设您的数据经过精心整理,一种快速而肮脏的方法是将对象的值组合成字符串,并用它们创建一个Set集合本质上不能有重复项。但与此同时,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))

关于javascript - 从解析的 CSV 数组中删除重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54162407/

相关文章:

javascript - 下拉框一直向上滚动

javascript - jquery 的 ajax 全局变量

javascript - 为什么我可以调用 [].any 方法却不能调用 {}.any 方法?

c++ - C:将字节数组转换为结构

d3.js - 在聚集条形图上使用居中 svg 图像

加拿大省的 Json 文件

database - 将数据保存/存储/写入数据库的 native d3.js 方法?

javascript - Google map API 中的建议路线选项?

javascript Push inside init 方法未正确更新数组长度

arrays - 从对象数组中获取值