2
在一个分量I可使用过滤我阵列以下:Array.filter()在角2组分
// 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'。过滤器函数返回一个新数组,其中您在过滤器函数中传递的谓词为true。 – Christos
你的代码很好,可能有一些错字 – Niladri