首页 > 解决方案 > 使用 Lodash 的 For Loop 替代方案(React Native)

问题描述

我试图迭代一个数组并访问描述

大批 在此处输入图像描述

我正在像这样通过常规 for 循环迭代

 for (var index = 0; index <components.length; index++) {
    const msgs  = components[index].messages;
    for(var value = 0; value < msgs.length; value ++ ){
      console.log(msgs[value].description);
    }
  }

请让我知道我们如何通过在 react native 中使用 loadash 方法来简化它。

标签: arraysreact-nativesortinglodash

解决方案


这里使用的是 lodash

import {forEach} from 'lodash';


forEach(components,(component,index)=>{
  const msgs  = component.messages;
  forEach(msgs,(msg,value)=>{
    console.log(msg.description);
  });
});

推荐阅读