gpt4 book ai didi

javascript - 如何在数组中使用类似于 AngularJS 的 SQL 中的 'Like"子句的过滤器

转载 作者:行者123 更新时间:2023-11-30 20:49:41 24 4
gpt4 key购买 nike

有没有办法在 Angular5 中对数组使用 LIKE 过滤器?我只能对数组使用过滤器函数,但它需要匹配确切的过滤器才能获取元素。

this.myArray= this.myArray2.filter(myClass => myClass.name == value);

我想要的是如果 value == 'mar',它会返回数组中名称带有 'mar' 的所有元素(例如 Mark, Romar、Smarts 等)

最佳答案

使用字符串方法includes():

this.myArray= this.myArray2.filter(element => element.name.includes(value));

请参阅以下链接以了解有关 includes() 方法的信息:https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/String/includes

关于javascript - 如何在数组中使用类似于 AngularJS 的 SQL 中的 'Like"子句的过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48323835/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com