我想要循环遍历Immutable中的List
,我使用了List.map
来实现这个目的,虽然可以工作,但不是很好。有没有更好的方法?因为我只是检查数组中的每个元素,如果该元素符合我的规则,我就会执行一些操作,就像Array.forEach
一样,我不想像Array.map
那样返回任何东西。
例如,这是我目前的工作:
let currentTheme = '';
let selectLayout = 'Layout1';
let layouts = List([{
name: 'Layout1',
currentTheme: 'theme1'
},{
name: 'Layout2',
currentTheme: 'theme2'
}])
layouts.map((layout) => {
if(layout.get('name') === selectLayout){
currentTheme = layout.get('currentTheme');
}
});
List.find
类似于Array.filter
,这正是我所需要的 :) - Seven LeeList.find
返回第一个匹配项,而Array.filter
则提供了满足条件的所有元素列表。 - Mateus ZitelliList.find
就像Array.find
,而List.filter
就像Array.filter
。记住这点很重要。 - Mihail