简体   繁体   中英

javascript es6 destructuring

I understand that you can destructure function parameters. Is there a way to have the object variable as well as the destructured properties of the object? example below

function updateUser(user: { id, name }) {
    update(user, where: { id });
}

You can just add a variable to the end of the destructured list and that turns into your object name essentially:

Const obj = {a: 1, b:2, c:3}
Const { a, b, ...rest } = obj
rest.c === 3 //true

I don't think you can, that's violating the purpose of object destruction. But instead, you can do it like this if you really want the original object.

function updateUser(user) {
    const { name, id } = user;
    update(user, { id });
}

You can use immutable-helper instead. This library allows you to update your object using a syntax (inspired by MongoDB's query syntax) like your explanation on your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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