解构嵌套对象:如何获取父级及其子级值? [英] Destructuring nested objects: How to get parent and it's children values?

查看:346
本文介绍了解构嵌套对象:如何获取父级及其子级值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的函数中,我得到textarea对象,其属性为 current

In the below function, I get the textarea object with the property current .

这里,嵌套解构适用于开始结束变量。但当前变量不起作用。

Here, nested destructuring works with Start and End variables. But current variable doesn't work.

function someFunction({ current: { selectionStart: Start, selectionEnd: End } }, AppStateSetter) {

    // do something with current, Start, and End
}


推荐答案

第一次解构仅创建开始结束变量。如果你想创建当前作为变量,那么你需要再次声明它。

The first destructuring creates only Start and End variables. If you want to create current as a variable, then you need to declare it again.

function ({ current: { selectionStart: Start, selectionEnd: End }, current }, AppStateSetter) {

// do something with current , Start , and End

}

你可以在Babel编译器上测试它

此代码:

const object = {
  current: {
    selectionStart: "prop 1",
    selectionEnd: "prop2"
  }
}

const { current: { selectionStart: Start, selectionEnd: End } } = object;

获取trasnpiled为:

Gets trasnpiled to:

var object = {
  current: {
    selectionStart: "prop 1",
    selectionEnd: "prop2"
  }
};

var _object$current = object.current,
    Start = _object$current.selectionStart,
    End = _object$current.selectionEnd;

如您所见,当前变量是没有创建。

As you can see, current variable is not created.

这篇关于解构嵌套对象:如何获取父级及其子级值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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