2017-08-02 50 views
1

是否有可能只解构我所需要的值,而不是所有的人:JS解构一些变量,而不是其他的JS或部分拆解

当然
let {myVar, _ , lastVar} = {first:"I need this", second: "Not this", third:"I also need this"} 
+0

文档本可以在这里找到:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment – Zac

+0

您的对象既没有myVar也没有'lastVar'属性。你是否将数组与对象混淆? – Bergi

回答

2

是。

如果你有一个对象,如:{foo: 4, bar: 2},只有foo要求:

let { foo } = {foo: 4, bar: 2}; 

这也将工作:

let {first: first, third: third} = {first:"I need this", second: "Not this", third:"I also need this"} 
1

是,

let { a } = { a: 'a', b: 'b', c: 'c' } 
// a is 'a' 

let { a, ...rest } = {a: 'a', b: 'b'., c: 'c' } 
// a is 'a' 
// rest is { b: 'b', c: 'c' } 

[编辑 - 与你的价值观]

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 
+0

@downvoter心智添加评论,以帮助我改进我的答案? – Zac

+0

myvar和lastvar在哪里? –

+0

@KevinB当然在版本中加入以显示 – Zac

相关问题