是否有可能只解構我所需要的值,而不是所有的人:JS解構一些變量,而不是其他的JS或部分拆解
當然let {myVar, _ , lastVar} = {first:"I need this", second: "Not this", third:"I also need this"}
是否有可能只解構我所需要的值,而不是所有的人:JS解構一些變量,而不是其他的JS或部分拆解
當然let {myVar, _ , lastVar} = {first:"I need this", second: "Not this", third:"I also need this"}
是。
如果你有一個對象,如:{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"}
是,
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
文檔本可以在這裏找到:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment – Zac
您的對象既沒有myVar也沒有'lastVar'屬性。你是否將數組與對象混淆? – Bergi