Reputation: 58652
I want to send a data attribute to an API server only if the method is add
, if it's delete
, I don't don't want to send my data.
I have
var body =
{
id: 1,
method: method,
params: [
{
data: {
"key1" : "value1",
"key2" : "value2",
"key3" : "value3"
},
url: `/url/anything`
}
],
session: session,
verbose: 1
};
I tried
if(method == 'delete') {
_.pick(body.params[0], ['data']);
}
I also tried
if(method == 'delete') {
_.pick(body.params[0],'data');
}
For some reason, I still see that I still sending the data
.
How would one go about debugging this?
Upvotes: 0
Views: 1230
Reputation: 14927
use _.assign
:
var body =
{
id: 1,
method,
params: [
_.assign({url: `/url/anything`}, method === 'delete'
? null
: {
data: {
"key1" : "value1",
"key2" : "value2",
"key3" : "value3"
}
}
)
],
session,
verbose: 1
};
Upvotes: 1
Reputation: 59
You may need to do this.
if(method === 'delete') {
body.params[0].data = undefined;
}
In this case, we delete all of the content of data
by assign undefined
.
Upvotes: 0
Reputation: 4388
if you take a look at lodash pick documentation you'll see that it doesn't change the source object instead its create a new object from the source object properties and returns the new object , if you want to remove the data property from the source object , you can use the unset method from lodash which removes a property from an object or a set of props , also you can use the delete operator
Upvotes: 1