2

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?

jofftiquez
  • 7,548
  • 10
  • 67
  • 121
  • 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 – jofftiquez 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 – jofftiquez Aug 16 '16 at 12:08
  • Thanks guys! This is really mind blowing. LOL – jofftiquez 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

6
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
  • 23,282
  • 5
  • 60
  • 71
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
  • 914,110
  • 126
  • 1,211
  • 1,335
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
  • 16
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
  • 884
  • 9
  • 25