如何在数组中使用类似于 SQL for AngularJS 中的“Like”子句的过滤器
How to use filters in Arrays similar to 'Like" clause in SQL for AngularJS
有什么方法可以在 Angular5 中对数组使用 LIKE 过滤器吗?我只能对数组使用过滤器函数,但它需要匹配确切的过滤器才能获取元素。
this.myArray= this.myArray2.filter(myClass => myClass.name == value);
我想要的是,如果 value == 'mar'
,它会 return 数组中名称带有 'mar'
的所有元素(例如 Mark、Romar、Smarts 等)
使用字符串方法includes()
:
this.myArray= this.myArray2.filter(element => element.name.includes(value));
请参阅以下 link 了解 includes()
方法:https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/String/includes
有什么方法可以在 Angular5 中对数组使用 LIKE 过滤器吗?我只能对数组使用过滤器函数,但它需要匹配确切的过滤器才能获取元素。
this.myArray= this.myArray2.filter(myClass => myClass.name == value);
我想要的是,如果 value == 'mar'
,它会 return 数组中名称带有 'mar'
的所有元素(例如 Mark、Romar、Smarts 等)
使用字符串方法includes()
:
this.myArray= this.myArray2.filter(element => element.name.includes(value));
请参阅以下 link 了解 includes()
方法:https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/String/includes