d3 - 如果字符串类型为

时间:2017-11-18 09:00:05

标签: csv d3.js

我正在使用d3来进行csv文件的数据可视化。 csv看起来像这样:

UserId,Reputation,Name,Tags
22656,985942,Jon Skeet,"<c#><google-cloud-platform><google-cloud-datastore><google-authentication>,<msbuild><asp.net-core><mono><.net-core><travis-ci>,<.net><nuget><.net-core><visual-studio-2017><csproj>,<.net><md5>,<c#><wpf><xaml><globalization>,<java><c#><variables><object><reference>,<java><newline>,<c#><.net><debugging><compiler-errors><jit>,<git><git-reset><git-revert><git-clean>,<git>,<java><generics>                                                                                                                             "

我的index.html看起来像这样:

d3.csv("UsersFavTags.csv", function(data){
console.log(data[0]);
for(i=0;i<data.length;i++){
    data[i].Tags = createTagArray(data[i].Tags)
}
data.forEach(function(d){
    console.log("d: "+d);
    d.Tags = createTagArray(d.Tags);
    d.CountedTags = listOccurence(d.Tags);
    d.Tags = listUnique(d.Tags);
});
console.log(data);
});

但是,控制台中的输出如下所示:

{UserId: "22656", Reputation: "985942", Name: "Jon Skeet", "Tags ": "<c#><google-cloud-platform><google-cloud-datastore…                                "}

请注意,Tags"Tags "的字符串类型,这使我无法访问Tags的值

1 个答案:

答案 0 :(得分:0)

您可以使用"Tags "语法访问["Tags "]媒体资源:

&#13;
&#13;
var object = {UserId: "22656", Reputation: "985942", Name: "Jon Skeet", "Tags ": "some string"};

console.log(object["Tags "]);
&#13;
&#13;
&#13;

您还可以处理data并在最后创建没有空格的对象:

&#13;
&#13;
var data = [
{UserId: "22656", Reputation: "985942", Name: "Jon Skeet", "Tags ": "some string"},
 {UserId: "22657", Reputation: "985943", Name: "Jon Skeet II", "Tags ": "another string"},  
];

data.forEach(function(item){
  item.Tags = item["Tags "]; // store value to the propety without space
  delete item["Tags "]; // delete property with space
});

console.log(data);
&#13;
&#13;
&#13;