【问题标题】:how to pass Array of values to filter using pickBy method如何使用pickBy方法将值数组传递给过滤器
【发布时间】:2019-04-01 09:12:37
【问题描述】:

我在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 must be a string ,CurrencyId must be a number 的错误。 我想实现这一点,只要我传递任何值,它就会起作用。

【问题讨论】:

  • 什么是预期输出并分享您尝试过的代码。

标签: javascript node.js lodash


【解决方案1】:

这很奇怪。我刚刚尝试使用 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 中试过这个。也许浏览器里有东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    • 2023-01-05
    • 2018-04-14
    • 2018-02-17
    • 2017-04-28
    相关资源
    最近更新 更多