2017-08-02 44 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

相關問題