gpt4 book ai didi

javascript - 如何解构一个对象并排除一个属性?

转载 作者:搜寻专家 更新时间:2023-10-30 22:04:01 24 4
gpt4 key购买 nike

我想解构一个对象并排除一个属性。

这就是我现在正在做的:

 let x = {a:1, b:2, c:3};
let y = {...x};
delete y.c;
store.setState(y);

我找到一篇关于 ES7 的文章,其中指出有一个新功能可以排除属性。所以上面会这样写:

 let x = {a:1, b:2, c:3};
store.setState({c, ...x});

https://codeburst.io/use-es2015-object-rest-operator-to-omit-properties-38a3ecffe90

以上在 Angular 7 中不起作用,我收到以下错误:

error TS2663: Cannot find name 'c'. Did you mean the instance member 'this.c'?

我目前正在运行 TypeScript 3.1.6,我的 tsconfig.app.json 文件如下所示。

{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"module": "es2015",
"types": []
},
"exclude": [
"src/test.ts",
"**/*.spec.ts"
]
}

这是父文件。

{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"importHelpers": true,
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2017",
"dom"
]
}
}

现在我认为问题在于行 "lib": ["es2017"] 是针对 ES6 的。

我该怎么做才能启用此 ES7 功能,它是否会破坏将 ES5 作为 TypeScript 的编译输出?

最佳答案

只需更新您的代码行 - let y = {c, ...x};const {c, ...noC} = x;。所以你的代码将是 -

const x = {a:1, b:2, c:3};
const {c, ...noC} = x;

console.log(noC);

建议如果您以后不打算更新它,最好将x 设为常量。

关于javascript - 如何解构一个对象并排除一个属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53853598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com