是否有可能只解构我所需要的值,而不是所有的人: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