【问题标题】:Filtering an array of nested objects过滤嵌套对象数组
【发布时间】:2016-03-21 12:16:28
【问题描述】:

我正在尝试在 Angular2 中创建一个过滤器管道,它必须通过包含多个嵌套对象的数组进行过滤。这些对象来自 Salesforce,有时会包含嵌套对象,如下例所示:

Object {
    Id: "a0436000001awB5AAI",
    Name: "Some product",
    Store__c: "a0436000001awC2JJU",
    Product__c: "a0136000001UGzzAAG",
    Product__r: Object {
        Id: "a0136000001UGzzAAG",
        Name: "Parent product",
        ...
    },
    ...
}

正常的排序方法效果不佳,因为它们往往不会进入多个级别。我一直在尝试自己写,但我似乎无法弄清楚。这是我现在拥有的:

// # 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;

                        }
                    }

                }
            });
        }
    }

}

它现在根本不起作用。

【问题讨论】:

  • 你想用这个管道过滤器实现什么?
  • 您的代码建议您尝试像这样过滤:"*ngFor="#i of someArray | filter:'a0136000001UGzzAAG':'Id,Sotre__c' "。我是对的吗?另外,您使用的 'in' 运算符是错误的。在 'if (item[key] in filterKeys)' 行中,您正在使用它数组,而它应该与对象一起使用。
  • @Abdulrahman 你是完全正确的关于我如何尝试使用管道。
  • @A_Singh 我将过滤器附加到名为 queueD ilter 的 ngModel 并尝试过滤表中的记录

标签: javascript filter angular


【解决方案1】:

让你的代码正常工作所需要做的就是改变这一行:

if (item[key] in filterKeys) {

“in 运算符”用于检查对象上是否存在属性。

相反,对于您使用的数组:

if (filterKeys.indexOf(key) > -1) {

这是一个有效的plunker

【讨论】:

    【解决方案2】:

    我想通了。它并不完美,但可以完成工作:

    private flattenObject(ob) {
            var toReturn = {};
    
            for (var i in ob) {
                if (!ob.hasOwnProperty(i)) continue;
    
                if ((typeof ob[i]) == 'object') {
                    var flatObject = this.flattenObject(ob[i]);
                    for (var x in flatObject) {
                        if (!flatObject.hasOwnProperty(x)) continue;
    
                        toReturn[i + '.' + x] = flatObject[x];
                    }
                } else {
                    toReturn[i] = ob[i];
                }
            }
            return toReturn;
        };
    
        transform(value, args) {
    
            let filterKeys: string[];
            if (args[1]) {
                console.log(args[1]);
                if (typeof args[1] === 'string') {
                    console.log('string');
                    let parts = args[1].replace(' ', '').split(',');
                    filterKeys = parts;
                } else if (typeof args[1] === 'object') {
                    console.log('object');
                    filterKeys = args[1];
                }
            }
    
    
            if (!args[0]) {
                return value;
            } else if (value) {
    
                let retArr: Object[] = [];
    
                for (let obj of value) {
    
                    let filterable: Object = this.flattenObject(obj);
    
                        for (let key in filterable) {
    
                            key = String(key);
                            let val = String(filterable[key]);
    
                            if (val.indexOf(args[0]) !== -1) {
                                if (filterKeys && filterKeys.length > 0) {
                                    if (filterKeys.indexOf(key) > -1) {
                                        retArr.push(obj);
                                    }
                                }
                                else {
                                    return true;
                                }
                            }
    
                        }
    
                }
    
                return retArr;
    
            }
        }
    

    基本上只是展平对象,然后对其进行过滤。

    【讨论】:

      猜你喜欢
      • 2020-03-11
      • 2020-03-08
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 2020-09-17
      • 2019-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多