【问题标题】:How to filter an array of objects by filtering an array inside of it in javascript如何通过在javascript中过滤其中的数组来过滤对象数组
【发布时间】:2020-08-09 10:03:03
【问题描述】:

我正在尝试对我的应用进行高级搜索,它有一个 stores 对象数组,每个 store 都是一个对象,每个 store 都有一个包含该 store 项目的对象数组,(每个 item 都是目的)。 (我将保留 stores 数组,这样你就明白我的意思了)

所以基本上,我希望用户按商品名称过滤商店,但我被卡住了,我尝试的任何方法似乎都不起作用。 这是代码:

存储数组:

let stores = [
  {
    name:"",
    type:"",
    items:[
      {name:"tomato", quantity:"145", unit:"g"},  //this is what i want to filter with
      {name:"other items here", quantity:"45", unit:"kg"},
      {name:"example item", quantity:"74", unit:"l"},
    ]
  }
]

我试过的过滤方式:

let userInput = "tomato";


//this outputs the original array without any filtering

    let filteredStores = stores.filter(store=>{
      return store.items.filter(item=>{
        return item.name.includes(userInput)
      })
    })

希望有人理解我想如何过滤商店,谢谢

【问题讨论】:

  • 你的预期输出是什么?
  • 在这种情况下,用户输入“tomato”,输出应该是[{name:"tomato", quantity:"145", unit:"g"}]
  • 并且store数组中有多个对象?
  • @SajeebAhamed 是的
  • 如果商店看起来像这样-let stores = [ { name:"", type:"", items:[ {name:"tomato", quantity:"145", unit:"g"}, {name:"other items here", quantity:"45", unit:"kg"}, {name:"example item", quantity:"74", unit:"l"}, ] }, { name:"", type:"", items:[ {name:"tomato", quantity:"145", unit:"g"}, ] } ];

标签: javascript arrays object filter


【解决方案1】:

Array#filter 将在找不到匹配项时返回一个空数组。这是一个真实的值,你可以通过!!array.filter(() => false) 找到它。您需要在第二个过滤器上调用.length 才能确定它是否找到任何匹配项,0 是假的,其他的都是真的。

【讨论】:

  • 你的意思是这样的? return store.items.filter(item=>{ return item.name.includes("d").length }) 给了我同样的输入,你能帮忙吗
  • @SalahEddineMakdour - 没错。如果您想更明确,可以添加.length > 0
【解决方案2】:

let stores = [
  {
    name:"",
    type:"",
    items:[
      {name:"tomato", quantity:"145", unit:"g"},  //this is what i want to filter with
      {name:"other items here", quantity:"45", unit:"kg"},
      {name:"example item", quantity:"74", unit:"l"},
    ]
  }
]

let filterdStores = stores.filter(s=>s.items.some(i=>i.name==='tomato'));

console.log(JSON.stringify(filterdStores,null,2));

使用高效的Array.some

【讨论】:

  • @SalahEddineMakdour Array.some 会在找到匹配项时破坏内部块,因此在看到匹配项时不会搜索整个数组。它的性能更高。
  • 这个问题是,当页面加载时,它不会显示商店,直到我在搜索栏中输入 smth,所以当过滤字符串为空时,它会给出一个空数组,而我没有想要
  • @SalahEddineMakdour 您的 fetch 函数必须灵活且有条件!
【解决方案3】:

你可以试试这个:

let stores = [
  {
    name:"",
    type:"",
    items:[
      {name:"tomato", quantity:"145", unit:"g"},  //this is what i want to filter with
      {name:"other items here", quantity:"45", unit:"kg"},
      {name:"example item", quantity:"74", unit:"l"},
    ]
  },
 {
    name:"",
    type:"",
    items:[
      {name:"tomatos", quantity:"14", unit:"kg"},
    ]
  }
];

let UserInput = "tomato";

const res = stores.filter(({items}) => items.find(item => item.name.includes(UserInput)));

console.log(res);
.as-console-wrapper{min-height: 100%!important; top: 0}

【讨论】:

  • 在你的代码中,结果我只得到物品,我想得到有这些物品的整个商店,希望你理解
  • 在你的评论中你说输出是[{name:"tomato", quantity:"145", unit:"g"}]
  • 检查我更新的答案,我想这就是你想要的。
  • 哦,那是个错误,对不起,输出将是包含用户搜索项目的整个商店,对不起,这是一个错误
  • 现在商店只有 1 件商品,我想用他们的商品过滤商店并返回过滤后的商店而不做任何更改
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-12
  • 1970-01-01
  • 2021-04-23
  • 2022-01-25
  • 2018-11-22
  • 2020-07-03
相关资源
最近更新 更多