获取对象级联数组的indexOf



我已经试了好几个小时了,但我无法理解。

我在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需要一个元素。您可以使用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));

相关内容

  • 没有找到相关文章

最新更新