0

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

4
  • Why do you use .map()? Have you checked what this method does? Commented Jun 22, 2020 at 12:26
  • ya it returns an array of [key, value] for each iteration. Commented Jun 22, 2020 at 12:30
  • No, it returns whatever the callback returns. And your callback doesn't return anything. Commented Jun 22, 2020 at 12:31
  • okk, any idea how to do this then? Commented Jun 22, 2020 at 12:32

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

environment is an array of object, how can we directly take id from this?
@juhi Sorry didn't see that. I have edited the answer.
0

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;
      }
    });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.