【问题标题】:Assign options in destructure [duplicate]在解构中分配选项[重复]
【发布时间】:2020-12-25 08:12:21
【问题描述】:

我尝试根据 ES6 重写以下代码。我不断收到 ESLint 警告,到目前为止我已经花了大约 20 分钟,我不太确定如何编写它......

  .then(result => {
    const [categories, properties, placements] = result.map(r => r.data);

    this.properties = properties.map(property => {
      {
        ...property,
        category: categories.find(c => c.id === property.category_id),
        property: placements.filter(p => p.property_id === property.id),
      }
    });
  });

上面的代码根本没有解析,但根据我的尝试,它说我不能在箭头函数中使用 return {}。

如果我尝试只修改参数,则会收到no-param-reassign 的错误

【问题讨论】:

    标签: javascript ecmascript-6 eslint


    【解决方案1】:

    我意识到我可以运行 eslint fix 来看看它是如何完成的:

            this.properties = properties.map(property => ({
              ...property,
              category: categories.find(c => c.id === property.category_id),
              property: placements.filter(p => p.property_id === property.id),
            }));
    
    

    【讨论】:

    • 要从箭头函数返回一个对象,您需要将对象括在括号中(就像您在此处所做的那样)以避免与函数体混淆,或者在函数中使用 return 关键字身体。
    猜你喜欢
    • 2016-06-05
    • 1970-01-01
    • 2019-11-24
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    • 2020-12-20
    相关资源
    最近更新 更多