Filtering objects from one array that are present in another object

I have an array selectedTags which contains all the technology tags a user enters:

   0:
    id: "tech1"
    text: "tech1"
   1:
    id: "tech2"
    text: "tech2"

I also have a an array called rawProjects which contains the current projects.
There are multiple projects in rawProjects, each of which have a technology array associated with them.
Example, the technology array for one of the projects looks like:

0: {id: 21, project_id: 3, name: "tech1"}
1: {id: 22, project_id: 3, name: "c++"}

While for another one it looks like

0: {id: 23, project_id: 4, name: "tech55"}
1: {id: 24, project_id: 4, name: "java"}

How can I filter out any projects that have either of the technologies in the selectedTags array i.e in the end I want to rawProjects to only contain the project with project_id = 3 because thats the one which contains tech1

Each project in rawProjects has a technology array. I’m trying to find if either of these technology arrays contain any member of the selectedTags array

I think this should work!
If you really f**ked up with this problem then see this.

   const rawProjects_ = [
    {id: 21, project_id: 3, name: "tech1"},
    {id: 22, project_id: 4, name: "c++"},
    {id: 23, project_id: 5, name: "tech55"},
    {id: 24, project_id: 6, name: "java"}
]

const techStack_ = [
    {
         id: "tech1",
         text: "tech1"
    },
    {
         id: "c",
         text: "c"
    },
    {
         id: "c++",
         text: "c++"
    },
    {
         id: "tech2",
         text: "tech2"
    },
]

const newArray_ = rawProjects_.filter(pro => {
    return techStack_.find(({text}) => {
        return pro.name === text && pro 
    })
})

console.log(newArray_);

Hey, thanks for the response. However, rawProjects looks like this :


   const rawProjects_ = [
    {id: 1, name: "proj1", technology: 
 0: {id: 21, project_id: 1, name: "tech1"}
1: {id: 22, project_id: 1, name: "c++"}}  //end of project 1
   {id: 2, name: "proj2", technology: 
 0: {id: 23, project_id: 2, name: "tech14"}
1: {id: 24, project_id: 2, name: "java"}}  //end of project 2
]

So each project in rawProjects has a technology array and I’m trying to filter out projects that have either member of techStack in their technology array

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Is technology property an array of objects?

Screen Shot 2021-02-14 at 1.30.11 AM

Thats a technology array for one of the projects in rawProjects. It only contains one item right now in this example

Technology is an array of objects and technology is property of an object that is value of rawProjects array is I’m right?
something like this,

rawProjects = [
    {
         id: 1,
         name: "proj1",
         technology: [
             0: {id: 21, project_id: 1,name: "tech1"},
             1: {id: 22, project_id: 1,name: "c++"}
          ]
    },
    {
         id: 2,
         name: "proj2",
         technology: [
             0: {id: 23, project_id: 2,name: "tech14"},
             1: {id: 24, project_id: 2,name: "java"}
         ]
    }
]

Yup thats exactly right

Is you don’t get any error to use a number as a key of an object?

Hello, I just solved my problem. I used help of this link javascript - Filter array of objects by sub array of values - Stack Overflow

1 Like