javascript - 合并对象并删除属性
问题描述
假设我有一个像这样结构的对象数组
"err": [
{
"chk" : true,
"name": "test"
},
{
"chk" :true
"post": "test"
}
]
我怎样才能像这样重新构造它:
"err": [
{
"post": "test"
"name": "test"
}
]
我试过了
arr.filter(obj => delete obj.chk);
它可以成功删除chk
属性,但是如何将两个对象结合起来呢?
解决方案
您可以将它们传播Object.assign
到创建一个新对象,然后chk
从该对象中删除该属性:
const err = [
{
"chk" : true,
"name": "test"
},
{
"chk" :true,
"post": "test"
}
];
const newObj = Object.assign({}, ...err);
delete newObj.chk;
console.log([newObj]);
另一种不删除的方法是chk
在左侧进行解构,并使用 rest 语法:
const err = [
{
"chk" : true,
"name": "test"
},
{
"chk" :true,
"post": "test"
}
];
const { chk: _, ...newObj } = Object.assign({}, ...err);
console.log([newObj]);
推荐阅读
- python - 在python中将任意二进制整数除以3
- jmeter - 如何将jmeter仪表板报告转换为pdf
- vba - 通过遍历用户表单从模块设置用户表单宽度
- swift - 使用 SwiftUI 或 UIKit 定位视图元素?
- javascript - 如何将变量从 ts fie 转移到另一个 angular
- python - 在 Django 中使用 Celery Task 创建对象(在 ViewSet 类中)
- typescript - 通过打字稿中的动态键字符串访问反应原生样式表成员
- c - CS50 Speller.c 问题
- kubernetes - kubectl apply 与 kubernetes 部署 - Terraform
- ubuntu - 是否有一个快速脚本可以将文件夹中的 JPG 重命名为其 HSL 值