renathy
renathy

Reputation: 5345

how to remove property from javascript without loosing it in original object

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

Upvotes: 0

Views: 57

Answers (1)

Typo
Typo

Reputation: 1900

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

Upvotes: 1

Related Questions