在Array Angular上逐字过滤

时间:2018-01-16 20:36:14

标签: javascript angularjs typescript lodash typescript2.0

我正试图在数组上逐个字母地过滤

MyJSON

"mainArr":[  
       {  
          "data":false,
          "myArr":[  
             {  
                "myNumber": 12345,
             },
             {
                "myNumber": 8961,
             }
          ]
       }
    ]

以下是我在下面搜索但不能根据字母按键或按键或离子输入选择对象的代码

    if (filterVal && filterVal.trim() !== '') {
        this.myArray = this.myArray.filter(function (item) {
          const results: any = [];
          const obj = item.mainArr[0];
          if (_.has(obj, 'myArr')) {
            console.log(obj.myArr.indexOf(filterVal));

            for (let i = 0; i < obj.myArr.length; i++) {
               if (obj.myArr.indexOf(filterVal) !== -1)
                 return results.push(obj.myArr[i]);
               }
            }
          }
        });
      }

Refered URL

1 个答案:

答案 0 :(得分:0)

尝试了另一种实现方法,但是backspace没有重新绑定到原始数​​组

let results: any = [];
events.subscribe('filterSearch', (filterVal: string) => {
  if (filterVal && filterVal.trim() !== '') {
    results = Object.assign(this.allEventsSorted.filter(function (item: any): any {
      const fltDty = item.mainArr[0];

      if (_.has(fltDty, 'myArr')) {
        return _.some(
          item.mainArr[0].myArr,
          function (tag): any {
            return _.startsWith(tag.myNumber, filterVal);
            // return _.includes(tag.myNumber, filterVal);
          }
        );
      }
    }));
    this.allEventsSorted = results;
  }
});