【问题标题】:get the indexOf of a cascaded array of objects获取级联对象数组的 indexOf
【发布时间】:2022-01-15 10:21:43
【问题描述】:

我正在尝试和困惑几个小时,但我无法理解它。

我在 Redux 中有如下数据结构:

  entities: {
    users: {
      dentists: [
        {
          id: 1,
          first_name: 'Tessa',
          last_name: 'Iiannone',
          phone: '+234 325 319 4277',
          email: 'tiiannone0@dentistcompanybvt.com'
        },
        {
          id: 2,
          first_name: 'Kennett',
          last_name: 'Pedreschi',
          phone: '+48 204 144 9885',
          email: 'kpedreschi1@dentistcompanybvt.com'
        },
        {
          id: 3,
          first_name: 'Lorine',
          last_name: 'Tolle',
          phone: '+670 691 959 9810',
          email: 'ltolle2@dentistcompanybvt.com'
        },
        {
          id: 4,
          first_name: 'Nessi',
          last_name: 'Pikhno',
          phone: '+995 756 907 2258',
          email: 'npikhno3@dentistcompanybvt.com'
        }
      ],
      assistants: [
        {
          id: 1,
          first_name: 'Nickolas',
          last_name: 'Seamans',
          phone: '+62 949 597 4013',
          email: 'nseamans0@dentistcompanybvt.com'
        },
        {
          id: 2,
          first_name: 'Peri',
          last_name: 'Helversen',
          phone: '+51 886 232 9275',
          email: 'phelversen1@dentistcompanybvt.com'
        }
      ],
      clients: [
        {
          id: 1,
          first_name: 'Mona',
          last_name: 'Shakelade',
          phone: '+63 475 243 2059',
          email: 'mshakelade0@sourceforge.net',
          date_of_birth: '26/01/1987',
          status: null
        },
        {
          id: 2,
          first_name: 'Dario',
          last_name: 'Aizikovitz',
          phone: '+33 454 959 7355',
          email: 'daizikovitz1@buzzfeed.com',
          date_of_birth: '16/08/1999',
          status: null
        },
        {
          id: 3,
          first_name: 'Caren',
          last_name: 'Chidgey',
          phone: '+358 905 256 6974',
          email: 'cchidgey2@imgur.com',
          date_of_birth: '08/03/1983',
          status: null
        },
        {
          id: 4,
          first_name: 'Timmi',
          last_name: 'Weond',
          phone: '+225 796 207 5915',
          email: 'tweond3@dion.ne.jp',
          date_of_birth: '25/08/1972',
          status: null
        },
        {
          id: 5,
          first_name: 'Greer',
          last_name: 'Cornelius',
          phone: '+46 793 784 2482',
          email: 'gcornelius4@ask.com',
          date_of_birth: '29/03/1968',
          status: null
        },
        {
          id: 6,
          first_name: 'Catlee',
          last_name: 'Elmar',
          phone: '+33 826 857 9849',
          email: 'celmar5@economist.com',
          date_of_birth: '25/11/1976',
          status: null
        },
        {
          id: 7,
          first_name: 'Ilsa',
          last_name: 'Tynnan',
          phone: '+591 283 830 4992',
          email: 'itynnan6@illinois.edu',
          date_of_birth: '19/02/1992',
          status: null
        },
        {
          id: 8,
          first_name: 'Delia',
          last_name: 'Blueman',
          phone: '+55 392 389 4499',
          email: 'dblueman7@printfriendly.com',
          date_of_birth: '09/11/1975',
          status: null
        },
        {
          id: 9,
          first_name: 'Lorilyn',
          last_name: 'Semens',
          phone: '+7 271 804 0493',
          email: 'lsemens8@zimbio.com',
          date_of_birth: '17/03/2001',
          status: null
        },
        {
          id: 10,
          first_name: 'Lorilee',
          last_name: 'Slemmonds',
          phone: '+63 858 699 0861',
          email: 'lslemmonds9@umich.edu',
          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'
        }
      }
    }
  }
}

我将切片中的 3 个列表与:

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

所以现在我希望当我说:

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

...会给我与action.payload.id 匹配项的索引,但是当我执行console.log(index) 时,我会继续获得-1,这意味着没有匹配项。

谁能帮帮我?

我做错了什么?

【问题讨论】:

  • .indexOf(searchElement, fromIndex): " indexOf() 方法返回可以在数组中找到给定元素的第一个索引,如果是,则返回-1不存在。”
  • 你想要.findIndex()

标签: javascript redux react-redux react-bootstrap


【解决方案1】:

indexOf 需要一个元素。可以使用find先获取元素,再获取索引:

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

findIndex 需要回调,这是您的情况的要求,因为您要查找您不知道但可以通过 id 找到的对象:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-22
    • 2017-05-31
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    相关资源
    最近更新 更多