1

I have an object which which has three different arrays like location vertical and roundType, and i will get a filter object that will have the same three arrays in that object. This is data that needs to be filtered

testObject = [{
    "id": 1892928,
    "vertical_tax": [
      678,
      664
    ],
    "location_tax": [
      666
    ],
    "roundType": [
      "rt1"
    ],
}
{
    "id": 1892927,
    "vertical_tax": [
      662,
      663
    ],
    "location_tax": [
       663
    ],
    "roundType": [
      "rt2"
    ],
}]

This is the filter object based on which the filtering should happen

 filterObject = {
    locations: [666,667]
    roundTypes: ["rt1","rt2"]
    verticals: [662,661]
   }

Original requirements : was to get any of objects with a particular value in either of the filter object arrays. This could be done with use of "some". Update requirements : So i need to filter the main object with the values passed in the filterObject. So all the conditions in filterObject should be matched. All the ids that match should be returned. this can be done with "every"

3
  • Can you reframe the question ? Commented Apr 29, 2020 at 15:16
  • So i need to filter the main object with the values passed in the filterObject. So it would be like if filterobject has location:[666,667] then in the main object all those ids which contain 666 in their location array should be returned Commented Apr 29, 2020 at 15:17
  • This would be simpler is the properties matched Commented Apr 29, 2020 at 15:29

2 Answers 2

2

You can filter on the objects and just exclude them. I've commented out part of the comparisons, because it's not clear if you want to filter on those properties, and how. You only mentioned locations. If you want it to include all matching results for all the properties, change the && to ||.
As has been mentioned, the code could be simplified and generalized if the properties matched (or had consistent naming).

filterObjects filters on any match being present.
filterObjects1 requires presence of all elements in verticals, and any match in the other properties.

testObject = [{
    "id": 1892928,
    "vertical_tax": [
      678,
      664
    ],
    "location_tax": [
      666
    ],
    "roundType": [
      "rt1"
    ],
},
{
    "id": 1892927,
    "vertical_tax": [
      662,
      663
    ],
    "location_tax": [
       663
    ],
    "roundType": [
      "rt2"
    ],
}]

 filterObject = {
    locations: [666,667],
    roundTypes: ["rt1","rt2"],
    verticals: [662,661]
   };
   
   const filterObjects = (filterObject, testObject) => {
    return testObject.filter(obj => 
      obj.location_tax && obj.location_tax.some(
        x => filterObject.locations && filterObject.locations.includes(x)) ||
      obj.roundType && obj.roundType.some(
        x => filterObject.roundTypes && filterObject.roundTypes.includes(x)) ||
      obj.vertical_tax && obj.vertical_tax.some(
        x => filterObject.verticals && filterObject.verticals.includes(x))
    );
   };
   console.log(filterObjects(filterObject, testObject));


 filterObject = {
    roundTypes: ["rt1","rt2"],
    verticals: [662,661]
 };

   console.log(filterObjects(filterObject, testObject));

   // require presence of all objects in filterObject.verticals using .every
   const filterObjects1 = (filterObject, testObject) => {
    return testObject.filter(obj => 
      (obj.location_tax && obj.location_tax.some(
        x => filterObject.locations && filterObject.locations.includes(x)) ||
       obj.roundType && obj.roundType.some(
        x => filterObject.roundTypes && filterObject.roundTypes.includes(x)) 
      ) &&
      filterObject.verticals.every( x => obj.vertical_tax && obj.vertical_tax.includes(x) )
    );
   };

 filterObject = {
    roundTypes: ["rt1","rt2"],
    verticals: [662,663]
   };
   delete testObject[0].vertical_tax;
  
   console.log(filterObjects1(filterObject, testObject));

// must match some value in all filterObject properties that exist

testObject = [{
    "id": 1892928,
    "vertical_tax": [
      678,
      664
    ],
    "location_tax": [
      666
    ],
    "roundType": [
      "rt1"
    ],
},
{
    "id": 1892927,
    "vertical_tax": [
      662,
      663
    ],
    "location_tax": [
       663
    ],
    "roundType": [
      "rt2"
    ],
}]

 filterObject = {
    locations: [666,667],
    roundTypes: ["rt1","rt2"],
//    verticals: [662,661,678]
   };
   
   // _must_ match some value in _all_ filterObject properties _that exist_
   
   const filterObjects = (filterObject, testObject) => {
    return testObject.filter(obj => 
      (!filterObject.locations || obj.location_tax && obj.location_tax.some(
        x => filterObject.locations && filterObject.locations.includes(x))) &&
      (!filterObject.roundTypes || obj.roundType && obj.roundType.some(
        x => filterObject.roundTypes && filterObject.roundTypes.includes(x))) &&
      (!filterObject.verticals || obj.vertical_tax && obj.vertical_tax.some(
        x => filterObject.verticals && filterObject.verticals.includes(x)))
    );
   };
   console.log(filterObjects(filterObject, testObject));

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

13 Comments

yea it is for all three properties like locations, verticals and roundTypes. One more question what if the one of the property does'nt have a value filterObject = { locations: [666,667], roundTypes: [], verticals: [662,661] };
inclusive OR, or they need to match all properties? as for the properties not having value, will need to add a check to make sure the property is defined, or it depends on how you want it to behave. if it is null or undefined, you want it to just not filter it at all or what?
need to match atleast one property.... how to add that check , because get an undefined error
I've updated the code, you'll see I add a check for each filterObject property. I didn't add the check for testObject, but it's similar
okay so one more question what if the results need to have 662 and 663 to be there. Basically all the properties needs to be matched
|
0

If you want to test only a single criteria, just use that statement and discard the rest, or if you want to have either of the conditions be true to get the result then change the && logic to || for the necessary statements.

testObject.filter( i => {
return i.vertical_tax.every((value, index) => value === filterObject.verticals[index]) &&
  i.location_tax.every((value, index) => value === filterObject.locations[index]) && 
i.roundType.every((value, index) => value === filterObject.roundTypes[index]);
});

2 Comments

yea it is for all three properties like locations, verticals and roundTypes. One more question what if the one of the property does'nt have a value filterObject = { locations: [666,667], roundTypes: [], verticals: [662,661] };
the comparison statements will just skip through that since there is nothing to compare against, there wont be any errors if values are missing

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.