简体   繁体   English

从列表列表创建 JSON 对象

[英]Creating a JSON object from a List of Lists

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));

I am trying to make an array of objects where each object has the "Name" which is the first element of the array, and then the value associated with the value either "ALLOW" or "Deny".我正在尝试创建一个对象数组,其中每个对象都有“名称”,它是数组的第一个元素,然后是与“允许”或“拒绝”值关联的值。 For example I would like to get:例如我想得到:

{name: "default_PROJECT", connect: "Allow", AddComment: "Allow"} ... etc 

However some of the arrays have have duplicate keys and if that value is Deny it will always trump a previous value of Deny.但是,某些数组具有重复的键,如果该值为 Deny,它将始终胜过先前的 Deny 值。

I started with iterating over each array and then trying to push following element has the key?我开始迭代每个数组,然后尝试推送以下元素是否有键? Am I on the write track?我在写作轨道上吗?

 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;
 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;}
 <div id="result1"></div>


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM