I'm filtering some arrays of objects by keyword using the JavaScript Array Filter. Initial, I had an array of objects:
let works = [
{ title: ' work 1', medium: 'tax1' },
{ title: ' work 2', medium: 'tax2' },
{ title: ' work 3', medium: 'tax3' },
{ title: ' work 4', medium: 'tax2' },
{ title: ' work 5', medium: 'tax1' }
]
And could filter the array by the 'medium' in this manner:
mediumFilter(taxonomy) {
return works.filter(item => {
return (
item.medium.indexOf(taxonomy) > -1
);
});
},
When I pass the taxonomy variable to the mediumFilter(), in this case 'tax1' or so -- I get an array that includes the items containing that specific medium.
This was initial -- now that my project is getting more complex, I find myself struggling. My new array includes a nested array, which are the values to match in the filtering.
let works = [
{ title: ' work 1', medium: [ 'tax1', 'tax3' ] },
{ title: ' work 2', medium: [ 'tax1' ] },
{ title: ' work 3', medium: [ 'tax3' ] },
{ title: ' work 4', medium: [ 'tax2', 'tax1' ] },
{ title: ' work 5', medium: [ 'tax1' ] }
]
Notice how the medium is an array holding multiple strings.
I'm looking for some assistance on how to approach this. I need my function to return items that match the variable im passing. As some works could have multiple mediums, I need to be able to filter by multiple values. If the array of mediums has multiple values, only one needs to match.
Thanks!
btw im using vue which is not relevant to the question but syntax of my examples might not be exact.
mediumFilter(['tax3','tax2'])?