简体   繁体   中英

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.

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>


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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