简体   繁体   English

ES6中的高级对象分解

[英]Advanced Destructuring of objects in ES6

I have the following object: 我有以下对象:

personObj = {
  _id : '123',
  first_name: 'John',
  last_name: 'Doe',
}

I would like to destructure it to the following variables: 我想将其分解为以下变量:

id,  <-- _id
name: { 
  first,  <-- first_name
  last    <-- last_name
}

(I want first_name and last_name to reside inside a 'name' object) (我希望first_name和last_name驻留在“名称”对象中)

I've tried the following syntax: 我尝试了以下语法:

const {
  id: _id,
  name: {
    first: first_name,
    last:  last_name
  }
} = personObj

However this causes an error. 但是,这会导致错误。 What am I doing wrong? 我究竟做错了什么?

Update 更新

Chapter 10. Destructuring of book "Exploring ES 6" provides many advanced examples of how to use destructuring and explains how it works internally. 《探索ES 6》一书的第10解构提供了许多有关如何使用解构的高级示例,并解释了其内部工作方式。

Destructuring can extract values directly into the properties of an object. 解构可以将值直接提取到对象的属性中。 The properties are not required to exist but all destination objects must already exist when the destructuring assignment happens. 这些属性不需要存在,但是在进行销毁分配时,所有目标对象必须已经存在。

Armed with this knowledge, the code that answers the question is: 有了这些知识,回答问题的代码就是:

let personObj = {
    _id: '123',
    first_name: 'John',
    last_name: 'Doe',
}

// Create the objects that receive the values on destructuring
let c = { name: {} }

// Do the magic
({ _id: c.id, first_name: c.name.first, last_name: c.name.last } = personObj)

console.log(c)
// {id: "123", name: {first: "John", last: "Doe"}}

The parentheses around the assignment expression that uses destructuring are required, without them the engine reports a syntax error at the first : . 必须使用使用解构的赋值表达式周围的括号,如果没有括号,引擎将首先报告语法错误:

The original answer follows. 原始答案如下。 It doesn't completely answer the question but I leave it here for reference. 它不能完全回答问题,但我在这里留作参考。 It shows how to use the rest properties ( ... ) in destructuring expressions and it was accepted by the OP, as incomplete as it is. 它显示了如何在解构表达式中使用rest属性( ... ,并且OP接受了它,因为它实际上是不完整的。


The original answer 原始答案

Destructuring with properties renaming works the other way around: the original name is placed before the colon, the new name is after it. 使用属性重命名进行解构的方法与此相反:原始名称位于冒号之前,新名称位于冒号之后。

let personObj = {
    _id: '123',
    first_name: 'John',
    last_name: 'Doe',
}

// Destructure personObj using different names for the properties
const {
    _id: id,
    first_name: first,
    last_name: last
} = personObj


console.log('id: ' + id);
console.log('first: ' + first);
console.log('last: ' + last);

// Output
//    id: 123
//    first: John
//    last: Doe

You can then assemble the pieces ( id , first , last ) into a new object: 然后,您可以将片段( idfirstlast )组装成一个新对象:

let c = {
    id,
    name: {
        first,
        last
    }
}

console.log(c);

// Output
//    { id: '123', name: { first: 'John', last: 'Doe' } }

Update 更新

The most similar result to what you describe in the question can be achieved by: 与您在问题中描述的最相似的结果可以通过以下方式获得:

let { _id: id, ...name } = personObj

console.log(id)
console.log(name)
// Output
//    123
//    { first_name: 'John', last_name: 'Doe' }

But this way the properties of name use the same names they have in personObj . 但是通过这种方式, name的属性将使用与personObj相同的名称。 Even more, it doesn't work any more if you add to personObj properties after last_name that you don't want to copy in name . 更重要的是,如果您在last_name之后添加了不想复制到name personObj属性,它将不再起作用。

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

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