简体   繁体   English

是否可以在销毁另一个对象时分配一个对象(在销毁时使用默认值)

[英]Is it possible to assign an object while destructuring another (WHILE using defaults in destructuring)

My end goal is to be able to assign a new object using the spread operator from an object that is destructured while assigning default values (if they don't exist). 我的最终目标是能够使用散布运算符从分配默认值(如果不存在)的同时被解构的对象中分配一个新对象。

It looks like this may not be possible how I'd like it. 看来我可能无法做到这一点。 Here's my expectations and attempts: 这是我的期望和尝试:

 //Expected beginning object const a1 = { key1: "test1", key2: "test2", }; //Expected end result const b1 = { key1: "test1", key2: "test2", key3: { nestedKey1: "nestedVal1", }, }; //Expected beginning object const a2 = { key1: "test1", key2: "test2", key3: { nestedKey1: "actualValue", } } //Expected end result const b2 = { key1: "test1", key2: "test2", key3: { nestedKey1: "actualValue", }, }; 

Snippet 1: Does not assign default values. 片段1:不分配default值。

 const a = { key1: "test1", key2: "test2", } const {...b} = { key1, key2, key3: { nestedKey1: nestedKey1 = "nestedVal1", } = {}, key4 = 'someDefault' } = a console.log(b); // does not have key3, or key4 console.log(key4); //this exists as a const, key3 does not 

Snippet 2: Functional, but can become problematic if multiple levels of nesting are required. 片段2:功能正常,但如果需要多层嵌套,则可能会出现问题。

 const a = { key1: "test1", key2: "test2", } let { key1, key2, key3: { nestedKey1: nestedKey1 = "nestedVal1", } = {}, } = a const key3 = a.key3 || {}; // is it possible to include this in the destructuring? Specifically in the destructuring which defines the default. const b = { key1, key2, key3: { ...key3, nestedKey1, } } console.log(b); 

Snippet 2 (showing that nested objects aren't overwritten) 片段2(显示嵌套对象没有被覆盖)

 const a = { key1: "test1", key2: "test2", key3: { someOtherKey: "itWorks", } } let { key1, key2, key3: { nestedKey1: nestedKey1 = "nestedVal1", } = {}, } = a const key3 = a.key3 || {}; const b2 = { key1, key2, key3: { ...key3, nestedKey1, } } console.log(b2); 

Your confusion arises from the similarity of destructuring syntax to that of object declaration syntax. 您的困惑来自解构语法与对象声明语法的相似性。

This is object declaration: 这是对象声明:

const a = {
  key1: "test1",
  key2: "test2",
}

This is destructuring 这是破坏性的

const { key1, key2 } = a

console.log(key1)  // => test1
console.log(key2)  // => test2

Next, you need to remember that assignment operator = is right associative in JavaScript. 接下来,您需要记住,赋值运算符=在JavaScript中是正确关联的。 So something like 所以像

let a = b = 1

means assign value 1 to b , then to a . 表示将值1分配给b ,然后分配给a

Next, if you spread all values into a single var, you're essentially doing a simple assignment with a fancy ES6 syntax. 接下来,如果将所有值都分散到单个变量中,则实际上是在使用花式ES6语法进行简单分配。 So: 所以:

const {...b} = a
// is same as
const b = a

Combining the above 3 effects, what you get is effectively a multiple assignment: 结合以上3种效果,您将获得有效的多重分配:

const {...b} = a  
const {key1, key2} = a

Multiple assignment can be confusing and there are linting rules to prevent such issues: https://eslint.org/docs/rules/no-multi-assign 多重分配可能会造成混淆,并且存在防止这种问题的规则: https : //eslint.org/docs/rules/no-multi-assign

So the answer to your question is simply, No! 因此,您的问题的答案很简单:不! :) :)

It'd need at least 2 lines to do so - one to collect props with defaults while destructuring, and another to create the next object with those props. 为此,至少需要两行-一行在销毁时收集具有默认值的道具,另一行使用这些道具创建下一个对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM