【发布时间】:2022-12-08 01:06:25
【问题描述】:
我有一个对象,它有一些像这样的属性:
obj1={
"id": 2,
"description": "",
"operationIds": [
{
"id": 1,
"name": "Standard"
}
],
"ratingIds": [
{
"id": 1,
"name": "name1",
"description": "",
},
{
"id": 4,
"name": "name4",
"description": "",
},
{
"id": 8,
"name": "name8",
"description": "",
},
],
}
我想将对象内的对象数组(operationIds 和ratingIds)隐藏到属性数组中,我正在接收这个对象,我想对其应用更改并提供另一种方法,因此它应该如下所示:
obj1={
"id": 2,
"description": "",
"operationIds": [
1
],
"ratingIds": [
1,
4,
8
],
"timestamp": "AAAAAAAGJ6c=",
"estimatedUtilReconciliationApplies": true
}
我能够做到,但是以一种非常丑陋的方式,有没有更简单干净的方法来完成这个?
let x = {...obj} as any;
let ar1 = x.operationIds;
const arr1= ar1.map(function (obj) {
return obj.id;
});
let ar2 = x.ratingIds;
const arr2= ar2.map(function (obj) {
return obj.id;
});
x.operatingEnvironmentIds = arr1;
x.thrustRatingIds = arr2;
【问题讨论】:
-
是什么让你的方法“丑陋”?
-
您的解决方案非常好。
-
忘记命名我只是在测试,但它很长,我想知道是否有一个简单而简短的解决方案。
标签: javascript angular typescript