juhi
juhi

Reputation: 578

check element in array of objects

i have a javascript array of objects like this:

public clientList = [
    {
      name: 'LA Care Health Plan',
      representative: {
        name: 'Brittany Bennet',
        imageUrl: '../../../../../../assets/images/clients/5.jpg',
      },
      team: [
        {
          name: 'Chris',
          imageUrl: '../../../../../../assets/images/clients/1.jpg',
        },
        {
          name: 'David',
          imageUrl: '../../../../../../assets/images/clients/2.png',
        },
        {
          name: 'Mary',
          imageUrl: '../../../../../../assets/images/clients/3.png',
        },
        {
          name: 'John',
          imageUrl: '../../../../../../assets/images/clients/4.jpg',
        },
      ],
      environment: [
        {
          id: 1,
          name: 'Staging',
          services: ['File submission', 'Bulk update'],
        },
        {
          id: 2,
          name: 'Production',
          services: ['Workflow Building', 'Workflow Building'],
        },
        {
          id: 3,
          name: 'Development',
          services: ['Workflow Building', 'Workflow Building'],
        },
      ],
    },
    {
      name: 'Chicago State Health Dermatology department',
      representative: {
        name: 'Amanda Sue',
        imageUrl: '../../../../../../assets/images/clients/2.png',
      },
      team: [
        {
          name: 'Stacy',
          imageUrl: '../../../../../../assets/images/clients/10.jpg',
        },
        {
          name: 'John',
          imageUrl: '../../../../../../assets/images/clients/11.jpg',
        },
      ],
      environment: [
        {
          id: 10,
          name: 'Staging',
          services: ['File submission', 'Bulk update'],
        },
        {
          id: 12,
          name: 'Production',
          services: ['Workflow Building', 'Workflow Building'],
        },
        {
          id: 12,
          name: 'Development',
          services: ['Workflow Building', 'Workflow Building'],
        },
      ],
    },
  ];

on click of a radio button, I get the object of environment key like

{
              id: 12,
              name: 'Development',
              services: ['Workflow Building', 'Workflow Building'],
            },

now I have to see if that object exists in the clientList array if it does then I have to return complete object and mark a variable as true

I have tried doing this but no success.....

this.clientList.map((x) => {
      x.environment.find((id) => id.id === event.id);
    });

any idea what I am doing wrong here, can someone help

Upvotes: 0

Views: 49

Answers (2)

Jon
Jon

Reputation: 166

You could use the function filter for that purpouse. Doing this:

const searchedClient = this.clientList.filter(client => client.environment.filter(eachEnv => eachEnv.id === event.id).length > 0);

searchedClient will be an array with your wanted client object in the first position or an empty array if no id is equal

Upvotes: 1

Naruto
Naruto

Reputation: 72

If you want to filter the clientList Object then use filter instead of map. If i am not wrong this is what you want.

    this.clientList = this.clientList.filter(client => {
      if (x.environment.find((id) => id.id === event.id)) {
        return client;
      }
    });

Upvotes: 0

Related Questions