Home > Back-end >  Find matching items in array of object values from array of filter criteria
Find matching items in array of object values from array of filter criteria

Time:11-29

I have some data (json) that looks like this:

[
    {
        "name": "Enterprise Networking",
        "technology": "Networking"
    },
    {
        "name": "Enterprise Networking",
        "technology": "Networking"
    },
    {
        "name": "Wireless Insights",
        "technology": "Mobility and Wireless"
    },
    {
        "name": "Mobility Insights",
        "technology": "Mobility and Wireless"
    },
    {
        "name": "Lock it down",
        "technology": "Security"
    },
    {
        "name": "Collaboration",
        "technology": "Security"
    }

]

I am trying to find matches based on an array of filtering options for one field named 'technology' in this example. For example, my filtering criteria will be ["Networking", "Security"]. I would expect to return all objects that have 'Networking' and 'Security' as their technology, excluding 'Mobility and Wireless' in this example.

I have figured out how to do it with just one filter criteria using:

result = learningMapsDataObj.filter(el => el.Technology === filter3[0].value);

I've tried adding a method that will loop through the the filter array, but can't get it to work.

result = learningMapsDataObj.filter(el => el.Technology === myloopingArrCheckFunction(el.technology, filters3));

I know I'm close, but haven't had to dev in a while and my multidimensional brain isn't working.

CodePudding user response:

let result = learningMapsDataObj.filter(el => el.technology === "Networking" || el.technology === "Security" );

CodePudding user response:

You can filter the array with an array of criterias like this

const filterCriteria= ["Networking", "Security"]
const filteredArray = learningMapsDataObj.filter((item) => filterCriteria.includes(item.technology));

if you want to set a variable for fields you want to filter, you can also do something like this

const filterField = 'technology'
const filterCriteria= ["Networking", "Security"]
const filteredArray = learningMapsDataObj.filter((item) => filterCriteria.includes(item[filterField]));

CodePudding user response:

Approaches

You can check if el.technology is included in your array of values to filter by:

const allowedTechnologies = ["Networking", "Security"];
learningMapsDataObj.filter(el => allowedTechnologies.includes(el.technology))

Alternatively, you can also check if el.technology is like some of the values to filter by:

const allowedTechnologies = ["Networking", "Security"];
learningMapsDataObj.filter(el => { // Linebreak (thus function body) for readability
  return allowedTechnologies.some(allowedTech => allowedTech === el.technology);
})

Map to string array

It seems your filter is in a form similar to this:

const filter = [
  { value: "Networking" }.
  { value: "Security" }
];

But it would be easier to use if it was in the form as shown in my examples above. You can achieve that by mapping the filter elements to their values:

const filter = [
  { value: "Networking" }.
  { value: "Security" }
];
const filterValues = filter.map(el => el.value);
// Now you can use filterValues like allowedTechnologies

CodePudding user response:

You can do it in various ways:

Using .includes()

const filter = ["Networking", "Security"];

const data = [ { "name": "Enterprise Networking", "technology": "Networking" }, { "name": "Enterprise Networking", "technology": "Networking" }, { "name": "Wireless Insights", "technology": "Mobility and Wireless" }, { "name": "Mobility Insights", "technology": "Mobility and Wireless" }, { "name": "Lock it down", "technology": "Security" }, { "name": "Collaboration", "technology": "Security" } ];

const filtered = data.filter(it => filter.includes(it.technology));
console.log(filtered);

Using Set()

Here you can construct a Set in O(n) time and later look for a key in O(1), so it's useful when you have large amounts of keys to compare.

const filter = new Set(["Networking", "Security"]);

const data = [ { "name": "Enterprise Networking", "technology": "Networking" }, { "name": "Enterprise Networking", "technology": "Networking" }, { "name": "Wireless Insights", "technology": "Mobility and Wireless" }, { "name": "Mobility Insights", "technology": "Mobility and Wireless" }, { "name": "Lock it down", "technology": "Security" }, { "name": "Collaboration", "technology": "Security" } ];

const filtered = data.filter(it => filter.has(it.technology));
console.log(filtered);

  • Related