gpt4 book ai didi

javascript - 如何解构 es6 javascript 嵌套对象

转载 作者:行者123 更新时间:2023-12-02 00:05:43 24 4
gpt4 key购买 nike

我想在 es6 中构造对象,但没有得到结果。

let animal ={
data:{
typee:{
title: "Cow",
legs:4
}
}
}

let {data:{typee:{title,legs}}}=animal;

现在console.log(data)给出输出错误:数据未定义。我做错了什么?

最佳答案

解构嵌套对象时,临时值不​​会分配给常量/变量。您必须明确分配它们:

const animal = {"data":{"typee":{"title":"Cow","legs":4}}};

const {
data, // assign the data
data: {
typee, // assign the typee
typee: {
title,
legs
}
}
} = animal;

console.log(data, typee, title, legs);

关于javascript - 如何解构 es6 javascript 嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59087851/

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