如何使用pickBy方法传递值数组以进行过滤

问题描述 投票:0回答:1

我在pickBy函数中传递一个数组作为参数

    return fetch(API_BASE + url + (params ? '?' + stringify(pickBy(params, v=>v===false||!!v)) : ''), {
    method: 'GET',
    headers,
    credentials: 'include'
  }).then(resp=>{
    authGuard(resp);
    return resp;
  });

而我的params对象是

 currencyId: [12,44]
    dueDateEnd: null
    dueDateStart: null
    exceptionTypeIds: (2) ["Contract updates/correction WIP", "Closed Contract"]

现在每当我只传递一个exceptionTypeIds然后它正在工作但是当我传递两个值时,它抛出的错误是exceptionTypeIds必须是一个字符串,CurrencyId必须是一个数字。每当我传递任何值时,我都希望实现它。

javascript node.js lodash
1个回答
0
投票

那真是怪了。我刚刚尝试使用lodash 4.14.120并且行为符合预期:

const params = {
    currencyId: [12, 44],
    dueDateEnd: null,
    dueDateStart: null,
    exceptionTypeIds: ["Contract updates/correction WIP", "Closed contract"]
}

console.log(_.pickBy(params, v => v === false || !!v))

回报

{
    currencyId: [ 12, 44 ],
    exceptionTypeIds: [ 'Contract updates/correction WIP', 'Closed contract' ]
}

即使只有一个exceptionTypeId或一个空数组:没有错误。

我在Node.js REPL中试过这个。也许浏览器中有一些东西。

© www.soinside.com 2019 - 2024. All rights reserved.