0

I am implementing a filter. it works fine. the problem is it's just matching a single object value instead of all value matching.

Matching means here is, let it be contain any single letter in the value

example: here is my object

{name:"D",color:"Green",size:50}

in case if i pass the filter object as :

let filter1 = {color:"Blu",size:'50'};

at present I am getting single result by matching size. But the color is not matching at all. so the result should be empty.

How to mach all values in the object and get the filtered value.

Live Demo

Code :

const nestedFilter = (targetArray, filters) => targetArray.filter(o => Object.keys(filters).find(k => filters[k].includes(o[k])));

let products = [
  {name:"A",color:"Blue",size:70},
  {name:"B",color:"Blue",size:60},
  {name:"C",color:"Black",size:70},
  {name:"D",color:"Green",size:50}
];

let filter1 = {color:"Blu",size:'50'};


console.log(nestedFilter(products, filter1));

2 Answers 2

1

Replace the .find invocation with .every. Be aware though that by using includes you expect your property values to be String.

If you want includes to work to other way round, so that the filter value can be a substring of the data, you should do:

const nestedFilter = (targetArray, filters) => 
    targetArray.filter(o => Object.keys(filters).every(k =>
        String(o[k]).includes(filters[k]))
    )
)

The o[k] value needs to be converted to string, as otherwise you cannot apply includes to it (cf. size which is a number)

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

4 Comments

I did, after I pass this value getting empty instead of 1. let filter1 = {color:"Gr",size:'50'};
If you want the includes to work, you should swap the direction of it. See addition to my answer. Note how includes is applies to the "longer" string: o[k]
I am not looking for ===, for me if partially contains also ok. Gr is partial here.
I see, see what I wrote and added.
0

Check whether every of the Object.entries of the filter passed is equal to the same entry on the object being iterated over. If you want partial matches and are using different types of variables, it sounds like you also need to coerce them to strings first, so you can use .includes.

const nestedFilter = (targetArray, filters) => targetArray.filter(
  obj => Object.entries(filters).every(
    ([key, val]) => String(obj[key]).includes(val)
  )
);

let products = [
  {name:"A",color:"Blue",size:70},
  {name:"B",color:"Blue",size:60},
  {name:"C",color:"Black",size:70},
  {name:"D",color:"Green",size:50},
  {name:"E",color:"Blu",size:'50'}
];

let filter1 = {color:"Blu",size:'70'};


console.log(nestedFilter(products, filter1));

2 Comments

If I pass let filter1 = {color:"Blu",size:'70'}; getting nothing. value can be partially contain fine for me. example for above i expected result as {name:"A",color:"Blue",size:70},
Sounds like you need to coerce the non-strings to strings too, if you want .includes to work with a partial match

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.