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);
5
  • 2
    You first need to clone the original object to new and then delete the property from it. Commented Oct 19, 2015 at 10:39
  • Possible duplicate of What is the most efficient way to clone an object? Commented Oct 19, 2015 at 10:39
  • @Typo This question is half of the answer. Commented Oct 19, 2015 at 10:40
  • @VisioN yep, but there's no half flagged option Commented Oct 19, 2015 at 10:41
  • @Typo Then you can use that question as a part of your answer or a comment for this question. Commented Oct 19, 2015 at 10:43

1 Answer 1

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

Comments

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.