【问题标题】:Replace _.forEach with something else用其他东西替换 _.forEach
【发布时间】:2016-07-03 15:24:03
【问题描述】:

我有问题。我制作了一个长函数,其中包含 4 个 _.forEach。我想用 lodash 中的其他东西或其他东西替换其中的一些 _.forEach。

所以,我有很多对象数组,我想遍历它们并比较它们的值。我已经做到了,但我想要一个更好的解决方案来拥有更简洁的代码。

这是我的功能:

function refreshFruits() {
     _.forEach($scope.fruits, function(fruit, index){
        _.forEach(vm.toUpdate.somethingLikeFruits, function(somethingLikeAFruit, dIndex) {
             .forEach(somethingLikeAFruit.P, function(dep, nIndex) {
                     _.forEach(fruit.l, function(leg, legindex) {
                                if(leg.Name === dep.LegName) {
                                    leg.Disc = dep.Discl;
                                    leg.Fore = dep.Projection;
                                }
                 });

          });

      });
  }); 

}

其中 fruits 是一个对象数组,vm.toUpdate.somethingLikeFruits 也是一个对象数组,P 和 l 是数组。

【问题讨论】:

  • 抱歉,这看起来像是学校的作业。
  • 请发布您的ArrayObjects 的样本以及预期结果。你可以看看 lodash find 方法。
  • 如果这是家庭作业,那么标记它。然后,您可以被引导自己思考,而不是“接受”最终解决方案而无需您做任何工作。

标签: javascript arrays foreach lodash


【解决方案1】:

将 refreshFruits() 函数拆分为更多函数。

喜欢:

function refreshFruite(){
}

function updateFruit(fruit){
}

function updateFruitValue(fruit, index){
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    • 1970-01-01
    • 2018-10-28
    • 2011-04-13
    • 1970-01-01
    • 2022-10-09
    相关资源
    最近更新 更多