【问题标题】:tslint error Use arrow function instead of function expressiontslint 错误使用箭头函数而不是函数表达式
【发布时间】:2019-03-05 22:55:29
【问题描述】:

我有 tslint 警告,我是 es6 的新手,但它的语法仍然有些困难。所以我不完全确定如何在箭头函数中转换它。

这是我的代码:

 let deletedQuestions = origQuestions.filter(function(obj) {
            return !updatedQuestions.some(function(obj2) {
                return obj.value == obj2.value;
            });
          });
          console.log(deletedQuestions);

          let addedQuestions = updatedQuestions.filter(function(obj) {
            return !origQuestions.some(function(obj2) {
                return obj.value == obj2.value;
            });
          });

【问题讨论】:

  • 您是否尝试过使用箭头函数语法?请向我们展示您的尝试。
  • 嗨 @Bergi 是的,我已经使用它作为我的功能之一 this.state.qnaActionHistory.filter(items => items.action === "add").map(qna => qna.qnaItem );这个基本上获得了添加项目操作的所有属性 qnaitem。但在我上面的问题中,我不确定如何在过滤器中执行和应用 if 和一些
  • 是的,那些看起来不错。尝试对问题代码中的 filtersome 参数使用相同的语法有什么问题?
  • "如何执行和应用 if" - 您不需要任何 if 语句吗?只需返回与当前函数表达式相同的值
  • 嗨,谢谢它现在开始工作 let deletedQuestions = origQuestions.filter( obj => !updatedQuestions.some(obj2 => obj.value == obj2.value));

标签: ecmascript-6 tslint


【解决方案1】:

我相信会是这样的

let deletedQuestions = origQuestions.filter(obj =>
  !updatedQuestions.some(obj2 => obj.value == obj2.value)) console.log(deletedQuestions);
let addedQuestions = updatedQuestions.filter(obj =>
  !origQuestions.some(obj2 => obj.value == obj2.value))

对吗?

【讨论】:

  • 啊是的,我终于用 let deletedQuestions = origQuestions.filter( obj => !updatedQuestions.some(obj2 => obj.value == obj2.value));这和你的一样!谢谢
猜你喜欢
  • 2019-08-13
  • 2016-06-03
  • 2020-11-30
  • 2018-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-27
  • 2012-09-30
相关资源
最近更新 更多