admin管理员组文章数量:1313613
Is it possible to destructure only the values I need and not all of them:
let {myVar, _ , lastVar} = {first:"I need this", second: "Not this", third:"I also need this"}
Is it possible to destructure only the values I need and not all of them:
let {myVar, _ , lastVar} = {first:"I need this", second: "Not this", third:"I also need this"}
Share
Improve this question
edited Aug 2, 2017 at 15:40
CommonSenseCode
asked Aug 2, 2017 at 15:36
CommonSenseCodeCommonSenseCode
25.4k38 gold badges148 silver badges195 bronze badges
2
- 1 Documentation for this can be found here: developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Zac Commented Aug 2, 2017 at 15:45
-
Your object has neither
myVar
norlastVar
properties. Are you confusing objects with arrays? – Bergi Commented Aug 2, 2017 at 17:55
3 Answers
Reset to default 4Yes of course.
If you have an object such as: {foo: 4, bar: 2}
, and only foo
is required:
let { foo } = {foo: 4, bar: 2};
This would also work:
let {first: first, third: third} = {first:"I need this", second: "Not this", third:"I also need this"}
You can easily rename destructured fields:
const o = {
first:"I need this",
second: "Not this",
third:"I also need this"};
const {first: myVar, third: lastVar, ...rest} = o;
//
console.log(` myVar - ${myVar}`);
console.log(`lastVar - ${lastVar}`);
console.log(` rest - ${JSON.stringify(rest)}`);
Yes,
let { a } = { a: 'a', b: 'b', c: 'c' }
// a is 'a'
or
let { a, ...rest } = {a: 'a', b: 'b'., c: 'c' }
// a is 'a'
// rest is { b: 'b', c: 'c' }
[edit - with your values]
let {first, third} = {first:"I need this", second: "Not this", third:"I also need this"}
// if you really want to change the variable names
let myVar = first, lastVar = third
版权声明:本文标题:javascript - JS Destructuring some variables but not others on js or partial destructuring - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741940635a2406121.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论