1
我使用的是request
npm模块,当我发出请求时,我将对象传递给null
键值,它将转换为''
,这对我来说是个错误。请求空值转换为字符串
request({
method: "POST",
url: "....",
form: {key: null},
json: true
}, callbackOnSuccess);
有人可以帮助我做到这一点?
我使用的是request
npm模块,当我发出请求时,我将对象传递给null
键值,它将转换为''
,这对我来说是个错误。请求空值转换为字符串
request({
method: "POST",
url: "....",
form: {key: null},
json: true
}, callbackOnSuccess);
有人可以帮助我做到这一点?
如果你需要对象属性的值更改从null
到undefined
,您可以使用lodash:
var newObject = _.mapValues(object, value =>
value === null ? undefined : value);
如果你有事情,如:
[{key: null}]
,那么你可能还需要遍历在阵列上:
var newAray = _.map(array, object =>
_.mapValues(object, value =>
value === null ? undefined : value));
您可能还需要重新完全取值为null
。
你可以做到很到位:
array.forEach(object => Object.keys(object).forEach(key => {
if (object[key] === null) delete object[key];
}));
或者你可以创建一个新的阵列和/或新的对象。
问题是'request'使用'querystring',所以'null'的值将会是''''。我正在寻找nodejs的查询字符串,但我没有发现任何内容:( – Davide
我认为如果你通过'undefined''而不是''null''参数将不会在请求中,所以你可以这样做''key:value || undefined'' – MatthieuLemoine
但我有数组的对象,我不想迭代数组并检查所有键。 – Davide
你能提供一个数组的代码示例吗? – MatthieuLemoine