如何按动态对象数组过滤数组? [英] How to filter array by array of dynamic objects?

查看:63
本文介绍了如何按动态对象数组过滤数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有700多个项目的大量数据收集,我想按对象的动态数组进行过滤,可以说用户键入了 ode ,并且我的脚本应该在没有i的情况下搜索每个过滤器做类似 filter.code == store.code

i have a huge collection of data 700+ items in, and i want to filter by dynamic array of objects, lets say that user has typed ode and my script should search on every filter without i do something like filter.code == store.code

const hugeData = [{name:"store 1",code : "code 1", available : false},{name : "store 2",code : "simple_code",available : true},{name : "sto 3",code : "has no cde",available : true}...]

const filters = [{ code : "ode", name : "re" }]

我期望的结果是

// output
[{name : "store 1", code : "code 1", available : false},{name : "store 2",code : "simple_code",available : true}]

我使用 lodash 解决了以下问题,但是功能非常慢,并且代码很差,还有其他方法可以做到这一点吗?

i resolved this doing the following with lodash, but function is very slow, and code is poor, is there any other way that i can do this works?

const results = []
_.map(hugeData, store => {
    _.map(filters, tag => {
        Object.keys(tag).map(tagOb => {
          if (store[tagOb] && store[tagOb].includes(tag[tagOb])) {
             results.push(store);
          }
        });
    });
});

推荐答案

您可以使用 filter some

const hugeData = [{ name: "store 1", code: "code 1",available: false}, { name: "store 2", code: "simple_code",available: true}, { name: "sto 3", code: "has no cde", available: true }]

const filters = { code: "ode", name: "re" }

let final = hugeData.filter(value => {
  return Object.entries(value).some(([key, value]) => {
    return typeof value == 'string' && value.includes(filters[key])
  })
})

console.log(final)

这篇关于如何按动态对象数组过滤数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆