1

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" }]

the result that im expecting is

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

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);
          }
        });
    });
});
8
  • couldn't filters just be a single object of key value pairs? why would it need to be in an array? Commented Oct 27, 2019 at 3:01
  • @Anthony yes, i can modify it, but if its a simple object, how that should work? Commented Oct 27, 2019 at 3:02
  • does multiple filters mean they all have to match? Commented Oct 27, 2019 at 3:05
  • what is ode ? Commented Oct 27, 2019 at 3:06
  • @Anthony not all have to match, with only one that matches, result should appear Commented Oct 27, 2019 at 3:06

2 Answers 2

3

You can use filter and 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)

Sign up to request clarification or add additional context in comments.

Comments

0

This might be faster - storing the Object.keys() result, and filtering out any keys that aren't on the first store (assuming all stores are equivalent). I also used a single object for filters.

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" };
const keys = Object.keys(filters).filter(k => hugeData[0].hasOwnProperty(k));

console.log(
  hugeData.filter(s => keys.some(k => s[k].includes(filters[k])))
);

If you wanted ALL filters, to match, you could change it to every:

hugeData.filter(s => keys.every(k => s[k].includes(filters[k])))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.