在TypeScript中组合map、filter和some

在TypeScript中组合map、filter和some,typescript,Typescript,我正在Angular 6中做一个应用程序,我想过滤一个对象数组,以确定其中一个对象的X属性是否等于X值 我的对象数组: users = [ { "id": "3myuB3YYlHNK5m4WZC7CxrX3MvA3", "identificationMethod": "cedula", "identificationNumber": "23447847457", }, { "id": "7ruDZvmsvTVZfA59nfB7SU65gwi1",

我正在Angular 6中做一个应用程序,我想过滤一个对象数组,以确定其中一个对象的X属性是否等于X值

我的对象数组:

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
测试。如果数组的第一个元素通过了测试,后一种方法将只查看第一个元素,而前一种方法将遍历所有元素

这在性能方面可能没什么大不了的,除非你的数组绝对庞大;在行为方面也没什么大不了的,除非你的数组在读取它们的时候有(比如,也许你使用了一个可以在读取时进行更改的数组)


无论如何,希望这有帮助;祝你好运

我不确定这是否需要充实成一个完整的答案,但我