0

How to remove property from javascript without loosing it in original object? I mean that I can do like this:

var originalObject = ...;

delete originalObject["Undefined"] and it would remove property originalObject.Undefined, however, I don't want originalObject to be changed. I wish like this:

newObject = removeUndefined(originalObject);
renathy
  • 4,686
  • 17
  • 79
  • 136

1 Answers1

1

From this question you have how to clone the object, as stated in one of the answers "Assuming that you have only variables and not any functions in your object":

you could define:

function removeUndefined(originalObject){

    var newObject = JSON.parse(JSON.stringify(originalObject));
    delete newObject['Undefined'];
    return newObject;
}

So later you could call:

newObject = removeUndefined(originalObject);
Community
  • 1
  • 1
Typo
  • 1,801
  • 1
  • 18
  • 31