我正在努力思考如何使用 Angular 管道根据另一个对象数组过滤一个对象数组。到目前为止,我拥有的是一个基于单个参数进行过滤的管道。
我有 2 个数组,数组 1 和数组 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);
}
}
最佳答案
如果数组 1 应该只包含数组 2 中的对象:
return array.filter(item => filterFrom.some(f => f.value == item.value));
如果数组 1 应该只包含数组 2 中相同索引处的对象:
return array.filter((item, index) => item.value == filterFrom[index].value);
关于arrays - Angular2使用管道基于对象数组过滤对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42028598/