【发布时间】:2020-08-06 21:50:37
【问题描述】:
我有一个“人类”类型的虚拟对象数组:
{
id: '3',
details: {
hair: 'blonde',
age: 20
}
我想得到同龄的第一个和最后一个人的索引。我对它寄予厚望
first = arr.map(element => element.details.age).findIndex(20);
还有
last = arr.lastIndexOf((element) => element.details.age === 20);
但是,它们都产生相同的错误:
Argument of type '(element: any) => boolean' is not assignable to parameter of type 'Human'.
Type '(element: any) => boolean' is missing the following properties from type 'Human': id, details
我要么需要有关如何使这些数组函数工作的帮助,要么需要一种不同的解决方案来获得相同的结果。
【问题讨论】:
-
你能分享更多你的代码吗?从这些行看起来是正确的,错误是否说明哪一行有问题?
标签: javascript arrays typescript indexing indexof