Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

javascript - 如何解构ES6 JavaScript嵌套对象(How to destructure es6 javascript nested objects)

I want to structure object in es6 but not getting results.(我想在es6中构造对象,但没有得到结果。)

let animal ={ data:{ typee:{ title: "Cow", legs:4 } } } let {data:{typee:{title,legs}}}=animal; now console.log(data) giving output Error: data is not defined .(现在console.log(data)提供输出Error: data is not defined 。) What I am doing wrong ?(我做错了什么?)   ask by Ashwani Panwar translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When destructuring nested objects, the interim values are not assigned to consts/variables.(销毁嵌套对象时,不会将中间值分配给const /变量。)

You'll have to assign them explictly:(您必须明确分配它们:) 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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...