Angular2基于使用管道的对象数组过滤对象数组

Ste*_*ons 1 arrays lambda filter typescript angular

我正在努力解决如何使用角度管道基于另一个对象数组过滤对象数组的问题.到目前为止我所拥有的是一个基于单个参数进行过滤的管道.

我有2个数组,array1和array 2,它们都包含复杂的对象.过滤后的数组(array1)应该只包含array1.value === array2.value的对象

我的代码到目前为止:

import { Injectable, Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'arrayFilter'
})
@Injectable()
export class AttributeFilterPipe implements PipeTransform {
  transform(array: any[], filterFrom: any[]): any {
    return array.filter(item => item.value.indexOf(filterFrom[0].value) !== -1);
  }
}
Run Code Online (Sandbox Code Playgroud)

tym*_*eJV 6

如果数组1应该只包含数组2中的对象:

return array.filter(item => filterFrom.some(f => f.value == item.value));
Run Code Online (Sandbox Code Playgroud)

如果数组1应该只包含在同一索引处的数组2中的对象:

return array.filter((item, index) => item.value == filterFrom[index].value);
Run Code Online (Sandbox Code Playgroud)