Javascript ES6 +:解构并同时使用数组方法? [英] Javascript ES6+: Destructuring and using an array method at the same time?

查看:183
本文介绍了Javascript ES6 +:解构并同时使用数组方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以同时破坏和使用数组方法?如果是,使用它是否有用,还是会大大降低代码的可读性,以致不值得?

I was wondering if there is a way, to destructe and use an array method at the same time? If yes, is it useful to use it, or would it decrease the code readabilty so much, that it's not worth it?

我当前的代码是这样:

const { props: { title, ingredients: ing } } = this;
const ingredients = ing.map(
  (ing, index) => <li key={index}>{ing}</li>
);

但是我正在尝试找到一种更短的方法:

But I'm trying to find a shorter way like this:

const { props: { title, ingredients: ingredients.map(
  (ing, index) => <li key={index}>{ing}</li>
); } } = this;

此代码无效.任何提示将不胜感激! :)

This code doesn't work though. Any tips would be much appreciated! :)

推荐答案

否,这是不可能的.销毁就是这样做的,它将属性分配给目标表达式.赋值语法没有任何可用于更改赋值的修饰符(默认初始化程序已经是拉伸).

No, this is not possible. Destructuring does just that, it assigns properties to target expressions. Assignment syntax does not have any modifiers for altering the assigned value (default initialisers are already a stretch).

按照@kingdaro的建议,使用

As @kingdaro suggested, use

const { title, ingredients } = this.props;
const ingredientElements = ingredients.map((ing, index) => <li key={index}>{ing}</li>);

这篇关于Javascript ES6 +:解构并同时使用数组方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆