-2

I have an array with objects:

const teams = [{
  TeamId: "asfqef",
  Projects: [{
    ProjectId: "wfwef",
    TeamId: "asfqef"
  }]
}];
projectCreated ={ProjectId: "aaaa", TeamId: "asfqef"};

so I need to push a Project into a especific Team, so for that I make It:

this.teams.forEach(team => {
  if (team.TeamId = projectCreated.TeamId ){
    team.Projects.push(projectCreated);
  }
});

My problem is that it is inserting the Project into all the Teams, so what's wrong with my logic?

critrange
  • 5,652
  • 2
  • 16
  • 47
user14860979
  • 107
  • 1
  • 2
  • 11
  • Does this answer your question? [In javascript == vs =?](https://stackoverflow.com/questions/11871616/in-javascript-vs) – VLAZ Mar 13 '21 at 07:24

1 Answers1

3

You are using = operator in the if statement. It will be always evaluated as true.

if ( team.TeamId === projectCreated.TeamId )

Update the code like the above.

critrange
  • 5,652
  • 2
  • 16
  • 47