我有像这样的对象的数组,
[
{
"link": "link1",
"model": "model1",
"role": "role1",
"access": "true"
},
{
"link": "link1",
"model": "model1",
"role": "role2",
"access": "true"
},
{
"link": "link1",
"model": "model1",
"role": "role3",
"access": "true"
},
{
"link": "link1",
"model": "model1",
"role": "role4",
"access": "true"
},
{
"link": "link1",
"model": "model2",
"role": "role1",
"access": "false"
},
{
"link": "link1",
"model": "model2",
"role": "role2",
"access": "false"
},
{
"link": "link1",
"model": "model2",
"role": "role3",
"access": "false"
},
{
"link": "link1",
"model": "model2",
"role": "role4",
"access": "false"
},
{
"link": "link2",
"model": "model1",
"role": "role1",
"access": "false"
},
{
"link": "link2",
"model": "model1",
"role": "role2",
"access": "true"
},
{
"link": "link2",
"model": "model1",
"role": "role3",
"access": "false"
},
{
"link": "link2",
"model": "model1",
"role": "role4",
"access": "true"
},
{
"link": "link2",
"model": "model2",
"role": "role1",
"access": "false"
},
{
"link": "link2",
"model": "model2",
"role": "role2",
"access": "true"
},
{
"link": "link2",
"model": "model2",
"role": "role3",
"access": "false"
},
{
"link": "link2",
"model": "model2",
"role": "role4",
"access": "true"
}
]
对于此question,输入是一个CSV样本,行数非常少。实际上我有一个大的CSV文件,我找到了一种方法来导入它并使用d3.js转换为JSON,但问题是我无法将其转换为所需的格式。我尝试使用data.forEach并且输出非常奇怪,我无法理解为什么我在两个链接中都单独model2
。
代码:
d3.csv('data.csv', function(data) {
var newData = {};
data.forEach(function(e, i) {
newData[e.link] = {};
newData[e.link][e.model] = {};
})
d3.select('main').append('pre')
.text(JSON.stringify(newData, null, ' '));
});
输出:
{
"link1": {
"model2": {}
},
"link2": {
"model2": {}
}
}
期望输出:
"link1": {
"model1": {
"role1": true,
"role2": true,
"role3": true,
"role4": true,
},
"model2": {
"role1": false,
"role2": false,
"role3": false,
"role4": false,
}
},
"link2": {
"model1": {
"role1": false,
"role2": true,
"role3": false,
"role4": true,
},
"model2": {
"role1": false,
"role2": true,
"role3": false,
"role4": true,
}
}
任何帮助将不胜感激。提前谢谢。
答案 0 :(得分:1)
在forEach中,您不会在每次迭代时分配角色和访问对,并且您总是清空每个部分。
用这个改变你的forEach ......
data.forEach(function(e, i) {
// Check if this "link" already exists exists, if not, create it.
if (!newData[e.link])
newData[e.link] = {};
// Check if this "model" already exists inside of this "link", if not, create it.
if (!newData[e.link][e.model])
newData[e.link][e.model] = {};
newData[e.link][e.model][e.role] = e.access;
});
我希望它有所帮助
答案 1 :(得分:0)
希望这会有所帮助
var data = [ { "link": "link1", "model": "model1", "role": "role1", "access": "true" }, { "link": "link1", "model": "model1", "role": "role2", "access": "true" }, { "link": "link1", "model": "model1", "role": "role3", "access": "true" }, { "link": "link1", "model": "model1", "role": "role4", "access": "true" }, { "link": "link1", "model": "model2", "role": "role1", "access": "false" }, { "link": "link1", "model": "model2", "role": "role2", "access": "false" }, { "link": "link1", "model": "model2", "role": "role3", "access": "false" }, { "link": "link1", "model": "model2", "role": "role4", "access": "false" }, { "link": "link2", "model": "model1", "role": "role1", "access": "false" }, { "link": "link2", "model": "model1", "role": "role2", "access": "true" }, { "link": "link2", "model": "model1", "role": "role3", "access": "false" }, { "link": "link2", "model": "model1", "role": "role4", "access": "true" }, { "link": "link2", "model": "model2", "role": "role1", "access": "false" }, { "link": "link2", "model": "model2", "role": "role2", "access": "true" }, { "link": "link2", "model": "model2", "role": "role3", "access": "false" }, { "link": "link2", "model": "model2", "role": "role4", "access": "true" } ]
var newData = {};
data.forEach(function(e, i) {
var keys = Object.keys(newData);
if(keys.indexOf(e.link)==-1){
newData[e.link] = {};
}
if(Object.keys(newData[e.link]).indexOf(e.model)==-1){
newData[e.link][e.model]={};
}
newData[e.link][e.model][e.role] = e.access;
})
console.log(newData);