Destructure a nested object, but keep a reference to the nested object [duplicate]

最后都变了- 提交于 2019-12-17 20:53:52

问题


I have a simple use-case, but I think it's not possible with ES6 syntax. I'd like to use object destructuring to retrieve certain known properties from a nested object, but I'd also like a reference to that nested object so that I can pass it along to other functions which may care about other properties.

Here's an example object:

var record = {
    name: "foo",
    metadata: {
        createdDate: "2017-02-19",
        lastModifiedDate: "2018-07-24",
        type: "bar"
    }
};

At a certain point in my code, I'd like to extract some values from the metadata. At the same time, I'd also like to extract the entire metadata object:

let {
    metadata: {
        createdDate
    }
} = record;

if ( date( createdDate ).before( NEW_FEATURE_DATE ) ){
    oldFeature( metadata );
} else {
    newFeature( metadata );
}

Unfortunately, my reference to the metadata property is used purely for destructuring... it's value is undefined (technically, being undefined, it has no value).

I know I can solve this with something like

let {
        metadata: {
            createdDate
        }
    } = record,
    metadata = record.metadata;

or

let {
        metadata
    } = record,
    {
        createdDate
    } = metadata;

... but that seems kind of clumsy, doesn't it? Is there something I'm missing, or is this just a "gap" in the syntax?


回答1:


You could just add another same property without destructuring.

var record = { name: "foo", metadata: { createdDate: "2017-02-19",  lastModifiedDate: "2018-07-24", type: "bar" } };

let { metadata: { createdDate }, metadata } = record;
//    ^^^^^^^^                                        for nested destructuring
//                               ^^^^^^^^             for the value

console.log(metadata);
console.log(createdDate);



回答2:


It depends on whether or not you want the nested object to also contain the destructured properties.

If you do then then see Nina Scholz's answer

If you don't then the use the ...rest syntax like so:

let {
    metadata: {
      createdDate,
      ...metadataExceptForCreatedDate
  }
} = record;


来源:https://stackoverflow.com/questions/51506671/destructure-a-nested-object-but-keep-a-reference-to-the-nested-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!