gpt4 book ai didi

javascript - 解构嵌套对象 : How to get parent and its children values?

转载 作者:数据小太阳 更新时间:2023-10-29 05:05:03 27 4
gpt4 key购买 nike

下面的函数接收一个对象,该对象具有属性 current,它也是一个对象,并且它具有 selectionStartselectionEnd 属性。

在这里,嵌套解构按预期使用 StartEnd 变量工作,但我还需要 current 的值。

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

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

我如何使用解构得到它?

最佳答案

第一个解构只创建StartEnd 变量。如果要将 current 创建为变量,则需要再次声明它。

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

// do something with current , Start , and End

}

您可以 test it on the Babel compiler :

这段代码:

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 变量。

关于javascript - 解构嵌套对象 : How to get parent and its children values?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54591307/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com