1

This is really weird I think, I have this json being sent through http.

{
   "foo":"bar",
   "foo2":"bar2",
   "name":{
       "firstName":"Joff",
       "middleName":"Ramirez",
       "lastName":"Tiquez"
   }
}

On the server I was performing these commands:

var data = req.body; // the json from http
console.log('data', data); // the data now has the req.body's value 
delete data.name; // <-- here's the delete
console.log('data', data); // the name object will obviously be deleted
console.log('req.body', req.body); // the name on the req.body was deleted too. Wtf?

So when I tried to use the req.body.name on the other parts of my program, the name is now gone. Is that how delete is supposed to work?

CENT1PEDE
  • 7,182
  • 9
  • 66
  • 120
  • 6
    Objects are passed by reference, `reqBody` is the same object as the `data` – Dellirium Aug 16 '16 at 12:00
  • WhyTH down vote? HAHA, It's a pretty valid question. lol – CENT1PEDE Aug 16 '16 at 12:05
  • @Dellirium what do you mean "passed by reference"? So am not creating a new object when I passed req.body to data? Wow, my whole life was a lie. LOL – CENT1PEDE Aug 16 '16 at 12:08
  • Thanks guys! This is really mind blowing. LOL – CENT1PEDE Aug 16 '16 at 12:12
  • 1
    When you "set the value" of a variable to an object, what you are doing is setting a pointer to a location in memory. So if you have an object, and you set `reqBody` to be that object, you are only copying the `address`. Then later you copy the address into `data`. After that you access `data` and remove the `name` from that address, but `reqBody` is still REFERENCING the same location in memory. There is only 1 object in your memory, and 2 variables each holding the same address to that object – Dellirium Aug 16 '16 at 12:15

4 Answers4

4
var data = JSON.parse(JSON.stringify(req.body));
delete data.name; // <-- here's the delete

Now when you do a

console.log('req.body', req.body); // This won't be deleted. 

As pointed out by @Dellirium, Objects are passed by reference, reqBody is the same object as the data

Thalaivar
  • 22,304
  • 5
  • 59
  • 68
3

Is that how delete is supposed to work?

Yes. delete deletes properties from objects.

… but that isn't where your confusion is coming from.

var data = req.body;

The assignment operator copies the value of req.body and that value is a reference to an object (JS only ever gives you a reference to an object to play with).

When you copy that reference to data you have two references pointing to the same object. When you delete a property from the object, it is removed from the object and it doesn't matter which reference to that object you use.

See this question for some information about making a deep copy of an object.

Community
  • 1
  • 1
Quentin
  • 857,932
  • 118
  • 1,152
  • 1,264
2

You need to save data in other variable 1 by 1 object if you want to delete in same way.

Because when you use any object to assign in other object then value assign by reference. for more please check- this link

For your problem solution put values 1 by 1.

Community
  • 1
  • 1
Mohan P
  • 422
  • 3
  • 15
1

Yes it is. Delete keyword deletes the property of the instance itself.

EDIT: Objects are passed by reference. Consider copying the object, so the changes you do will only affect the object you're changing.

Doruk
  • 864
  • 9
  • 25