Hoo*_*man 7 arrays components filter typescript angular
在一个组件中,我可以使用以下内容过滤数组:
// Array of product objects
const result = products.filter(p => p.name.includes('val'));
Run Code Online (Sandbox Code Playgroud)
产品的价值与第一价值相同,但过滤的价值存入result。
但是在下面的代码中,filter()过滤字符串数组本身:
// Array of strings
const result = strs.filter(s => s.includes('val'));
Run Code Online (Sandbox Code Playgroud)
问题是如何在不修改strs自身的情况下过滤字符串并返回结果?
注意:我尝试过array.filter(function() { return res; });但没有做任何更改。
它返回过滤后的值,并且不更改实际数组。你做错了
const strs = ['valval', 'bal', 'gal', 'dalval'];
const result = strs.filter(s => s.includes('val'));
console.log(strs);
console.log(result);Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17847 次 |
| 最近记录: |