【问题标题】:lodash findindex push to an arraylodash findindex 推送到数组
【发布时间】:2016-10-04 22:10:13
【问题描述】:

我正在使用_.findIndex,它返回一个数组,需要将其推送到数组。我该怎么做?

   $scope.filtersRequested[_.findIndex( $scope.filtersRequested, {
'codeColumnName': $scope.refData[idx].codeColumnName
                                       } )].filterCondition = strWhere;

【问题讨论】:

    标签: lodash


    【解决方案1】:

    如果我理解正确,您希望将 filterCondition 设置为特定值。由于您使用 lodash,因此您最好使用安全的 _.set(即,如果第一个 arg 未定义,则不会失败)和 _.find(以访问相关请求)。因此,我建议你这样做:

    _.set(
     _.find( $scope.filtersRequested, {'codeColumnName': $scope.refData[idx].codeColumnName} ) ,
     'filterCondition', strWhere
    );
    

    如果找到一个元素,_.set 将对它进行操作,否则,它将优雅地忽略它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-01
      • 2016-08-04
      • 2011-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-04
      相关资源
      最近更新 更多