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”,它是数组的第一个元素,然后是与“ALLOW”或“Deny”值相关联的值。例如,我想得到:
{name: "default_PROJECT", connect: "Allow", AddComment: "Allow"} ... etc
然而,有些数组具有重复键,如果该值为Deny,它将始终胜过之前的Deny值。
我开始迭代每个数组,然后尝试推送以下元素有关键?我在写作轨道上吗?
答案 0 :(得分:1)
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 result = [];
for(var i = 0, len = data.length; i < len; i++) {
var list = data[i];
result[i] = { name: list[0] };
for(var j = list.length - 1; j >= 1; j = j - 2) {
var key = list[j];
var value = list[j - 1];
console.log('calc', j, key, value);
result[i][key] = value;
}
}
/** IGNORE THIS, IS JUST FOR DEBBUGGING **/
var resultElement = document.getElementById('result1');
var tpl = '';
for(var t = 0, tLen = result.length; t < tLen; t++) {
var item = result[t];
tpl+= '<table>' +
'<thead>' +
'<tr><td colspan="2">' + item.name + '</td></tr>' +
'<tr><th>KEY</th><th>VAL</th></tr>' +
'</thead>' +
'<tbody>'
;
for(var key in item) {
if(!item.hasOwnProperty(key) || key === 'name') { continue; }
tpl += '<tr><td>'+ key +'</td><td>'+ item[key] +'</td></tr>';
}
tpl += '</tbody></table>';
}
resultElement.innerHTML = tpl;
&#13;
table { text-align: left; width: 100%; margin-bottom: 50px; border-collapse: collapse;}
td, th { width: 50%; border: 1px solid black; line-height: 1; padding:2px 10px;}
[colspan="2"] { color: blue; font-weight: bolder;text-transform: uppercase; text-align: center;}
&#13;
<div id="result1"></div>
&#13;