0

I want to delete a property object as if it never existed, the solution of void operator (void 0) and delete operator march but I would like another alternative using lodash for example.

My case :

//variable
this.project.monthlyRent = void 0
//Object
this.project.TypeChoose =  void 0
3
  • you can use for variable this.project.monthlyRent = undefined; and for object this.project.TypeChoose = {}; Commented Jul 14, 2017 at 12:33
  • @Shohel Yes it works, another alternative? Commented Jul 14, 2017 at 12:36
  • 1
    delete this.project.montlyRent Commented Jul 14, 2017 at 12:43

2 Answers 2

2

You can use the delete operator, although I don't particular recommend using that alternative. If you do use it, you need to be aware of it's limitations and how it behaves if you're using strict mode.

If you instead want an immutable alternative, you can use, like you say, lodash. What you'd do then is use _.omit (for creating a new object with the given properties omitted) or _.pick (for creating a new object with only the given properties included). Like this:

var obj = {
    firstname: "Nikolaj",
    lastname :"Larsen",
    age:99
};
var result = _.omit(obj , ['age']); 
// result: { firstname: .., lastname: .. }

and

var obj = {
    firstname: "Nikolaj",
    lastname :"Larsen",
    age:99
};
var result = _.pick(obj , ['firstname', 'lastname']); 
// result: { firstname: .., lastname: .. }

Like said, it's immutable so it doesn't change the old object.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this

Example 1

//variable
this.project.monthlyRent = undefined;
//Object
this.project.TypeChoose =  {};

Example 2

//variable
delete this.project.monthlyRent;

//Object
delete this.project.TypeChoose;

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.