admin管理员组文章数量:1323729
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 mands:
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?
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 mands:
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?
-
6
Objects are passed by reference,
reqBody
is the same object as thedata
– Dellirium Commented Aug 16, 2016 at 12:00 - WhyTH down vote? HAHA, It's a pretty valid question. lol – jofftiquez Commented Aug 16, 2016 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 Commented Aug 16, 2016 at 12:08
- Thanks guys! This is really mind blowing. LOL – jofftiquez Commented Aug 16, 2016 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 theaddress
. Then later you copy the address intodata
. After that you accessdata
and remove thename
from that address, butreqBody
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 Commented Aug 16, 2016 at 12:15
4 Answers
Reset to default 6var 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
Is that how delete is supposed to work?
Yes. delete
deletes properties from objects.
… but that isn't where your confusion is ing 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.
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.
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.
本文标签: javascript delete keyword not working properlyStack Overflow
版权声明:本文标题:javascript delete keyword not working properly - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742121713a2421738.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论