销毁嵌套对象:如何获取父级及其子级值? [英] Destructuring nested objects: How to get parent and its children values?

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

问题描述

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

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

在这里,嵌套解构可与 Start End 变量一起使用,但是我还需要 current 的值.

Here, nested destructuring works with Start and End variables, But I also need the value for current.

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

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

如何通过重组获得它?

推荐答案

第一个解构仅创建 Start End 变量.如果要创建 current 作为变量,则需要再次声明.

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

}

您可以此代码:

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

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

被交易到:

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

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

如您所见,未创建 current 变量.

As you can see, current variable is not created.

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

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