[Vuejs]-What would be the best way to check if any of the keys in an array of objects contain a value from an object of arrays array?

1πŸ‘

βœ…

I think with the below code snippet you can filter out those projects where any of the technologies are present in the arrOfObjs array’s name properties.

Using flatMap() you can get all the names into an array as strings first. Then using filter() as you wanted originally and with using some() and includes() combination just filter out the projects if any of the technologies elements are represented in names array like:

({technologies}) => technologies.some(t => names.includes(t))

If you would like to check if all the elements are present, then suggest to use every() like:

({technologies}) => technologies.every(t => names.includes(t))

Possible solution – representing the example with some():

const arrOfObjs = [{name: 'test1', image: 'testing1'}, {name: 'test2', image:'testing2'}],
      projects = [{name: "testproject",description: "lorem ipsum",technologies: ["test2", "test7", "test3"]}, {name: "atest",description: "Lorem ipsum", technologies: ["test1", "test2", "test5"] }];

const names = arrOfObjs.flatMap(e => e.name);
const result = projects.filter(
   ({technologies}) =>
      technologies.some(t => names.includes(t)
      // technologies.every(t => names.includes(t) // if all needed
   )
);

console.log(result);

I hope that helps!

πŸ‘€norbitrial

Leave a comment