0

I have json object which i need to set/override some properties.
This part is done (i too the solution from this answer)
My problem now is handle the case the path to change is not given (or empty.)In this case it shoudl just return an empty Obj which i could then handle and return an error message.

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] pass and set new value to the obj.
Changes[1] should instead set updatedJson to empty one, which it does but when i check if Object is empty, updatedJson is full again.
Can someone explain me why is this happening?
And how can i handle error like empty path to object's value?

3
  • Alert("error"); break; Commented Nov 19, 2016 at 12:56
  • @Jonasw if i mean use break in my set() function it will just get out of that if/else which it already does. If you mean to put break into the if/else to check if obj is empty, well it will not work since as i said at that point the object is full again so that condition is never true. Commented Nov 19, 2016 at 13:23
  • than may have a look at try catch trow ... Commented Nov 19, 2016 at 13:58

1 Answer 1

1

Try this:

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'
    })
  }
Sign up to request clarification or add additional context in comments.

2 Comments

unfortunately not :( Doing so my updatedJson become the nested object each time. So result is that at the end it save just the last nested Obj instead of keep track of the whole Json object
Look like is working like that! :D :D It took a little bit of workaround but at the end it worked :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.