在嵌套对象上设置属性时设置空对象并返回空对象

时间:2016-11-19 12:45:13

标签: javascript

我有json对象,我需要设置/覆盖一些属性。
这一部分完成了(我也是this answer的解决方案) 我现在的问题是处理没有给出改变路径(或为空)的情况。在这种情况下,它只是返回一个空的Obj然后我可以处理并返回错误消息。

var obj = { "users": {
              "profile": {
                 "name": "markus",
                 "age": 28 
              }
          }
}

var changes = [
    {
      path: ['users', 'profile', 'name'],
     changes: "Nino"
  },
  {
      path: [],
     changes: "fail"
  }

 ];

// Func to set new values
function set(jsonObj, path, value, updatedJson) {
  if(path.length === 0 || value.length === 0) {
    updatedJson = {};
    return updatedJson;
  }

  if(path.length === 1){
    updatedJson[path] = value;

  } else {
    for(var i = 0; i < path.length-1; i++) {
        var elem = path[i]; 
        if( !updatedJson[elem] ) { 
          updatedJson[elem] = {}
        }
        updatedJson = updatedJson[elem]; 
    }
    updatedJson[path[path.length-1]] = value;
  }

  return updatedJson;
}



  var updatedJson = Object.assign(obj);    
  changes.forEach( function(changeObj){
    var path = changeObj.path;
    set(obj, path, changeObj.changes, updatedJson);
  });

  // handle empty object case
  if(Object.keys(updatedJson).length === 0 && obj.constructor === Object){
    callback({
      success: false,
      message: 'File not updated. One or more property are incorrect.'
    })
  } else {
     callback({
      success: updatedJson,
      message: 'File was succefully updated'
    })
  }

Changes[0]传递并为对象设置新值。
Changes[1]应该将updatedJson设置为清空,但是当我检查对象是否为空时,updatedJson再次填满。
有人能解释我为什么会这样吗? 我怎样才能处理像对象的空路径这样的错误?

1 个答案:

答案 0 :(得分:1)

试试这个:

var obj = { "users": {
              "profile": {
                 "name": "markus",
                 "age": 28 
              }
          }
}

var changes = [
    {
      path: ['users', 'profile', 'name'],
     changes: "Nino"
  },
  {
      path: [],
     changes: "fail"
  }

 ];

// Func to set new values
function set(jsonObj, path, value, updatedJson) {
  if(path.length === 0 || value.length === 0) {
    updatedJson = {};
    return updatedJson;
  }

  if(path.length === 1){
    updatedJson[path] = value;

  } else {
    for(var i = 0; i < path.length-1; i++) {
        var elem = path[i]; 
        if( !updatedJson[elem] ) { 
          updatedJson[elem] = {}
        }
        updatedJson = updatedJson[elem]; 
    }
    updatedJson[path[path.length-1]] = value;
  }

  return updatedJson;
}


  var success = true;
  var updatedJson = Object.assign(obj);    
  changes.forEach( function(changeObj){
    var path = changeObj.path;
    var result = set(obj, path, changeObj.changes, updatedJson);
    if(Object.keys(result).length === 0 && result.constructor === Object)
      success = false;
  });

  // handle empty object case
  if(!success){
    callback({
      success: false,
      message: 'File not updated. One or more property are incorrect.'
    })
  } else {
     callback({
      success: updatedJson,
      message: 'File was succefully updated'
    })
  }