Search code examples
javascriptreduxreact-reduxreact-bootstrap

get the indexOf of a cascaded array of objects


I'm trying and puzzling for a couple of hours now and I can't get my head around it.

I have the following data structure in Redux:

  entities: {
    users: {
      dentists: [
        {
          id: 1,
          first_name: 'Tessa',
          last_name: 'Iiannone',
          phone: '+234 325 319 4277',
          email: '[email protected]'
        },
        {
          id: 2,
          first_name: 'Kennett',
          last_name: 'Pedreschi',
          phone: '+48 204 144 9885',
          email: '[email protected]'
        },
        {
          id: 3,
          first_name: 'Lorine',
          last_name: 'Tolle',
          phone: '+670 691 959 9810',
          email: '[email protected]'
        },
        {
          id: 4,
          first_name: 'Nessi',
          last_name: 'Pikhno',
          phone: '+995 756 907 2258',
          email: '[email protected]'
        }
      ],
      assistants: [
        {
          id: 1,
          first_name: 'Nickolas',
          last_name: 'Seamans',
          phone: '+62 949 597 4013',
          email: '[email protected]'
        },
        {
          id: 2,
          first_name: 'Peri',
          last_name: 'Helversen',
          phone: '+51 886 232 9275',
          email: '[email protected]'
        }
      ],
      clients: [
        {
          id: 1,
          first_name: 'Mona',
          last_name: 'Shakelade',
          phone: '+63 475 243 2059',
          email: '[email protected]',
          date_of_birth: '26/01/1987',
          status: null
        },
        {
          id: 2,
          first_name: 'Dario',
          last_name: 'Aizikovitz',
          phone: '+33 454 959 7355',
          email: '[email protected]',
          date_of_birth: '16/08/1999',
          status: null
        },
        {
          id: 3,
          first_name: 'Caren',
          last_name: 'Chidgey',
          phone: '+358 905 256 6974',
          email: '[email protected]',
          date_of_birth: '08/03/1983',
          status: null
        },
        {
          id: 4,
          first_name: 'Timmi',
          last_name: 'Weond',
          phone: '+225 796 207 5915',
          email: '[email protected]',
          date_of_birth: '25/08/1972',
          status: null
        },
        {
          id: 5,
          first_name: 'Greer',
          last_name: 'Cornelius',
          phone: '+46 793 784 2482',
          email: '[email protected]',
          date_of_birth: '29/03/1968',
          status: null
        },
        {
          id: 6,
          first_name: 'Catlee',
          last_name: 'Elmar',
          phone: '+33 826 857 9849',
          email: '[email protected]',
          date_of_birth: '25/11/1976',
          status: null
        },
        {
          id: 7,
          first_name: 'Ilsa',
          last_name: 'Tynnan',
          phone: '+591 283 830 4992',
          email: '[email protected]',
          date_of_birth: '19/02/1992',
          status: null
        },
        {
          id: 8,
          first_name: 'Delia',
          last_name: 'Blueman',
          phone: '+55 392 389 4499',
          email: '[email protected]',
          date_of_birth: '09/11/1975',
          status: null
        },
        {
          id: 9,
          first_name: 'Lorilyn',
          last_name: 'Semens',
          phone: '+7 271 804 0493',
          email: '[email protected]',
          date_of_birth: '17/03/2001',
          status: null
        },
        {
          id: 10,
          first_name: 'Lorilee',
          last_name: 'Slemmonds',
          phone: '+63 858 699 0861',
          email: '[email protected]',
          date_of_birth: '20/07/1991',
          status: null
        }
      ]
    },
    appts: {
      id: 1,
      date: '01012022',
      hour: 8,
      client_id: 1,
      dentist_id: 1,
      assistant_id: 1
    },
    ui: {
      ids: [
        1
      ],
      entities: {
        '1': {
          id: 1,
          usertype: 'client'
        }
      }
    }
  }
}

I combined the 3 lists in my slice with:

const users = { dentists: dentists, assistants: assistants, clients: clients };

So now i expect that when i say:

const index = users.clients.indexOf((user) => user.id === action.payload.id) 

...wil give me the index of the matching item with action.payload.id, but I'll keep on getting -1 when I do a console.log(index) what means that there is no match.

Can anyone help me out here?

What am I doing wrong?


Solution

  • indexOf requires an element. You can use find to get the element first and get the index next:

    console.log(users.clients.indexOf(users.clients.find(client => client.id === action.payload.id)));
    

    findIndex requires a callback, which is the requirement in your case, as you look for an object that you don't know, but can find by id:

    console.log(users.clients.findIndex((client) => client.id == action.payload.id));