I am trying to make a filter pipe in Angular2 which is going to have to filter through an array containing multiple nested objects. These objects are coming from Salesforce and sometimes will contain nested objects like the following example:
Object {
Id: "a0436000001awB5AAI",
Name: "Some product",
Store__c: "a0436000001awC2JJU",
Product__c: "a0136000001UGzzAAG",
Product__r: Object {
Id: "a0136000001UGzzAAG",
Name: "Parent product",
...
},
...
}
Normal sorting methods don't work very well because they don't tend to go multiple levels. I have been trying to write my own, but I can't seem to figure it out. Here is what I have right now:
// # Filter Array of Objects
@Pipe({ name: 'filter' })
export class FilterArrayPipe implements PipeTransform {
transform(value, args) {
let filterKeys: string[];
if (args[1]) {
let parts = args[1].replace(' ', '').split(',');
filterKeys = parts;
}
if (!args[0]) {
return value;
} else if (value) {
return value.filter(item => {
for (let key in item) {
if ((typeof item[key] === 'string' || item[key] instanceof String && item[key]) && (item[key].indexOf(args[0]) !== -1)) {
if (filterKeys && filterKeys.length > 0) {
if (item[key] in filterKeys) {
return true;
}
}
else {
return true;
}
}
}
});
}
}
}
It doesn't really work at all right now.