【发布时间】:2015-10-27 22:11:41
【问题描述】:
var data = [
[
"default_PROJECT",
"Allow",
"Connect",
"Allow",
"AddComment",
"Allow",
"Write",
"Allow",
"ViewComments",
"Allow",
"ExportData",
"Allow",
"ExportImage",
"Allow",
"ViewUnderlyingData",
"Allow",
"Read",
"Allow",
"ShareView",
"Allow",
"Filter"
],
[
"Allow",
"ExportImage",
"Allow",
"Write",
"Allow",
"ViewComments",
"Allow",
"ShareView",
"Allow",
"Filter",
"Allow",
"ExportData",
"Allow",
"Connect",
"Allow",
"Read",
"Allow",
"ViewUnderlyingData",
"Allow",
"AddComment",
"Allow",
"ViewComments",
"Deny",
"ExportData",
"Allow",
"AddComment",
"Deny",
"Write",
"Allow",
"Read",
"Deny",
"ExportXml",
"Deny",
"ShareView",
"Allow",
"Connect",
"Allow",
"ChangeHierarchy",
"Allow",
"WebAuthoring",
"Deny",
"ViewUnderlyingData",
"Deny",
"Filter",
"Deny",
"ExportImage"
]
];
var newObj = {};
for(i=0; i<data.length; i++){
//newObj['name'] = data[i][0];
for(j=1; j<data[i].length;j++){
newObj[data[i][j+1]] = data[i][j];
document.write(data[i][j] + "----");
}
}
document.write(JSON.stringify(newObj));
我正在尝试创建一个对象数组,其中每个对象都有“名称”,这是数组的第一个元素,然后是与“允许”或“拒绝”值关联的值。例如我想得到:
{name: "default_PROJECT", connect: "Allow", AddComment: "Allow"} ... etc
但是,有些数组有重复的键,如果该值为 Deny,它将始终胜过之前的 Deny 值。
我从遍历每个数组开始,然后尝试推动以下元素有关键吗?我在写轨道上吗?
【问题讨论】:
-
为什么数据一开始是这样设置的?有必要吗?
标签: javascript arrays json