在TypeScript中组合map、filter和some
我正在Angular 6中做一个应用程序,我想过滤一个对象数组,以确定其中一个对象的X属性是否等于X值 我的对象数组:在TypeScript中组合map、filter和some,typescript,Typescript,我正在Angular 6中做一个应用程序,我想过滤一个对象数组,以确定其中一个对象的X属性是否等于X值 我的对象数组: users = [ { "id": "3myuB3YYlHNK5m4WZC7CxrX3MvA3", "identificationMethod": "cedula", "identificationNumber": "23447847457", }, { "id": "7ruDZvmsvTVZfA59nfB7SU65gwi1",
users = [
{
"id": "3myuB3YYlHNK5m4WZC7CxrX3MvA3",
"identificationMethod": "cedula",
"identificationNumber": "23447847457",
},
{
"id": "7ruDZvmsvTVZfA59nfB7SU65gwi1",
"identificationMethod": "cedula",
"identificationNumber": "23232323232",
},
{
"id": "8IpMYYfy5dhBaR7QoQz4mXXLE1T2",
"identificationMethod": "passport",
"identificationNumber": "src34323",
}
]
我想找到所有拥有identificationMethod
等于passport的用户,然后我想知道这些用户中是否有一个拥有identificationNumber
等于23232的用户
现在,我有了这个片段:
const equalUsers: UserId[] = this.users.filter(user => user.identificationMethod === identificationMethod);
const isUnique: boolean = equalUsers.map(user => user.identificationNumber).some(value => value === identificationNumber);
(isUnique)
? console.log('Is unique')
: console.log('Is not unique');
我想实现同样的效果,但是将.map()
、.filter()
和.some()
(或任何其他可用方法)组合在一行中
我试过:
const isUnique = this.users.filter(user => user.identificationMethod === identificationMethod)
.some(user => user.identificationNumber === identificationNumber);
- 我需要在这里使用
吗.map()
- 有更好的办法吗
我的目标是实现与第一个代码片段相同的功能,但是更简短、更快、更容易理解。我很困惑,我不确定是否需要先使用
.map()
,还是不需要。可以在数组方法中扩展条件
const用户=[{
“id”:“3myuB3YYlHNK5m4WZC7CxrX3MvA3”,
“身份识别方法”:“护照”,
“识别号”:“23447847457”,
},
{
“id”:“3myuB3YYlHNK5m4WZC7CxrX3MvA3”,
“身份识别方法”:“护照”,
“识别号”:“23447847457”,
},
{
“id”:“8IpMYYfy5dhBaR7QoQz4mXXLE1T2”,
“身份识别方法”:“护照”,
“识别号”:“src34323”,
}
];
const identificationMethod=“passport”;
const identificationNumberDuplicate=“23447847457”;
const identificationnumbernunique=“src34323”;
const identificationNumberDoesNotExist=“xxx”;
常量测试=(identificationMethod,identificationNumber)=>{
const found=users.filter(user=>user.identificationMethod===identificationMethod&&
user.identificationNumber===identificationNumber);
const exists=found.length>0;
const isUnique=found.length==1;
log(“Exists:+Exists,”Unique:+isUnique);
};
试验(标识方法、标识编号等);
试验(标识方法、标识号副本);
测试(识别方法、识别号嗅探员)代码>您可以在数组方法中扩展条件
const用户=[{
“id”:“3myuB3YYlHNK5m4WZC7CxrX3MvA3”,
“身份识别方法”:“护照”,
“识别号”:“23447847457”,
},
{
“id”:“3myuB3YYlHNK5m4WZC7CxrX3MvA3”,
“身份识别方法”:“护照”,
“识别号”:“23447847457”,
},
{
“id”:“8IpMYYfy5dhBaR7QoQz4mXXLE1T2”,
“身份识别方法”:“护照”,
“识别号”:“src34323”,
}
];
const identificationMethod=“passport”;
const identificationNumberDuplicate=“23447847457”;
const identificationnumbernunique=“src34323”;
const identificationNumberDoesNotExist=“xxx”;
常量测试=(identificationMethod,identificationNumber)=>{
const found=users.filter(user=>user.identificationMethod===identificationMethod&&
user.identificationNumber===identificationNumber);
const exists=found.length>0;
const isUnique=found.length==1;
log(“Exists:+Exists,”Unique:+isUnique);
};
试验(标识方法、标识编号等);
试验(标识方法、标识号副本);
测试(识别方法、识别号嗅探员)代码>我不确定这是否需要充实到完整的答案中,但我建议这样做:
this.users.some(user =>
user.identificationMethod === identificationMethod &&
user.identificationNumber === identificationNumber
);
假设您有一个读取时不会抛出错误的有限长数组数组
,一个单参数函数f
,一个单参数p
(谓词是返回true
或false
的函数),以下表达式应始终给出相同的结果:
array.map(f).some(p); // turn all x into f(x), then check that at least one p(f(x)) is true
array.some(x => p(f(x))); // check that at least one p(f(x)) is true
array.filter(p).some(q); // of the x where p(x) is true, is there an x where q(x) is true?
array.some(x => p(x) && q(x)); // is there an x where both p(x) and q(x) are true?
也就是说,您可以将p
和f
放入一个新的谓词q
,只需执行array.some()
。你已经弄明白了,因为在你的情况下
f
是user=>user.identificationNumber
,
p
是id=>id==identificationNumber
,
所以q
是user=>user.identificationNumber===identificationNumber
此外,以下内容应始终提供相同的结果:
array.map(f).some(p); // turn all x into f(x), then check that at least one p(f(x)) is true
array.some(x => p(f(x))); // check that at least one p(f(x)) is true
array.filter(p).some(q); // of the x where p(x) is true, is there an x where q(x) is true?
array.some(x => p(x) && q(x)); // is there an x where both p(x) and q(x) are true?
也就是说,您正在通过将两个谓词p
和q
组合成一个新的谓词r
。对上述回调执行该转换:
p
是user=>user.identificationMethod===identificationMethod
,
q
是user=>user.identificationNumber===identificationNumber
,
因此r
是u=>user.identificationMethod===identificationMethod&&user.identificationNumber===identificationNumber
请注意,尽管它们给出相同的结果,但它们执行的步骤并不相同。您拥有的原始版本,array.filter(p).map(f).some(q)
,最终将对数组的每个元素x
执行p
,然后对所有通过测试的元素执行f
,最后对所有f(x)
执行q
,直到找到一个通过测试的元素。但是新版本,array.some(r)
只在元素上执行p
和f
,直到通过q
测试。如果数组的第一个元素通过了测试,后一种方法将只查看第一个元素,而前一种方法将遍历所有元素
这在性能方面可能没什么大不了的,除非你的数组绝对庞大;在行为方面也没什么大不了的,除非你的数组在读取它们的时候有(比如,也许你使用了一个可以在读取时进行更改的数组)
无论如何,希望这有帮助;祝你好运 我不确定这是否需要充实成一个完整的答案,但我