如何使用Lodash JavaScript过滤以多种条件(字符串和值数组)过滤收集

发布于 2025-02-04 03:42:06 字数 660 浏览 2 评论 0原文

我正在尝试使用Lodash进行多种条件过滤一个集合,它可以与字符串一起使用,但是我想在位置的一部分中滤除ID数组,并以状态字符串。我试图将ID更改为ID的“位置”:{“ ID”:[“ 1”,“ 4”]},,但它不起作用

const collection = [ 
  {account: {id: "1", name: "a"}, location:{id: "1", name: "x"}, status: "ok"},
  {account: {id: "2", name: "b"}, location:{id: "2", name: "x"}, status: "ok"},
  {account: {id: "3", name: "c"}, location:{id: "4", name: "y"}, status: "failed"},
  {account: {id: "4", name: "d"}, location:{id: "4", name: "y"}, status: "ok"},]

 let filterByConditions = {
  "location":{"id" :"1"},
  "account":{ "id": "1"},
  "status": "ok",
};

let results = _.filter(collection, filterByConditions);

I'm trying to filter a collection with multiple conditions using lodash, it works fine with strings, but I want to filter by an array of id's for the part of the locations and string for status. I have tried to change the id to array of id's "location":{"id" :["1","4"]}, but it doesn't work

const collection = [ 
  {account: {id: "1", name: "a"}, location:{id: "1", name: "x"}, status: "ok"},
  {account: {id: "2", name: "b"}, location:{id: "2", name: "x"}, status: "ok"},
  {account: {id: "3", name: "c"}, location:{id: "4", name: "y"}, status: "failed"},
  {account: {id: "4", name: "d"}, location:{id: "4", name: "y"}, status: "ok"},]

 let filterByConditions = {
  "location":{"id" :"1"},
  "account":{ "id": "1"},
  "status": "ok",
};

let results = _.filter(collection, filterByConditions);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百善笑为先 2025-02-11 03:42:06

看来您正在尝试使用 filter 使用“匹配”速记的方法。此速记不支持“或”条件;仅确切匹配。但是,您可以传递_。Filter方法a predicate ,它将起作用:

const collection = [ 
  {account: {id: "1", name: "a"}, location:{id: "1", name: "x"}, status: "ok"},
  {account: {id: "2", name: "b"}, location:{id: "2", name: "x"}, status: "ok"},
  {account: {id: "3", name: "c"}, location:{id: "4", name: "y"}, status: "failed"},
  {account: {id: "4", name: "d"}, location:{id: "4", name: "y"}, status: "ok"},]

let filterByPredicate = (elem) => {
  return elem.status === "ok" && 
    elem.location.id === "1" && 
    (elem.account.id === "1" || elem.account.id === "4");
};

let results = _.filter(collection, filterByPredicate);

It looks like you're trying to use the filter method with the "matches" shorthand. This shorthand doesn't support an "or" condition; only exact matches. However, you can pass the _.filter method a predicate instead, and it'll work:

const collection = [ 
  {account: {id: "1", name: "a"}, location:{id: "1", name: "x"}, status: "ok"},
  {account: {id: "2", name: "b"}, location:{id: "2", name: "x"}, status: "ok"},
  {account: {id: "3", name: "c"}, location:{id: "4", name: "y"}, status: "failed"},
  {account: {id: "4", name: "d"}, location:{id: "4", name: "y"}, status: "ok"},]

let filterByPredicate = (elem) => {
  return elem.status === "ok" && 
    elem.location.id === "1" && 
    (elem.account.id === "1" || elem.account.id === "4");
};

let results = _.filter(collection, filterByPredicate);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文