【发布时间】:2018-04-24 10:49:53
【问题描述】:
在一个组件中,我可以使用以下内容过滤我的数组:
// Array of product objects
const result = products.filter(p => p.name.includes('val'));
产品的值与第一个值相同,但过滤后的值存储在result。
但在以下代码中,filter() 过滤字符串数组本身:
// Array of strings
const result = strs.filter(s => s.includes('val'));
问题是如何在不修改strs 本身的情况下过滤字符串并返回结果?
注意:我尝试使用array.filter(function() { return res; });,但没有进行任何更改。
【问题讨论】:
-
此
strs.filter(s => s.includes('val'));不会修改strs。 filter 函数返回一个新数组,其中包含您在 filter 函数中传递的谓词为 true 的项目。 -
你的代码没问题,可能有错别字
标签: arrays angular typescript filter components