【发布时间】:2020-05-15 11:06:10
【问题描述】:
我有以下具有多个属性的对象
let features: {
durable: 5,
weight: 4
}
我需要将其转换为服务器接受的某种格式,如下例所示。上述features 值放入value 属性和features 属性名称(“耐用”等)放入category.name
"score": [
{
"value": 5,
"category": {
"name": "durable"
}
},
{
"value": 4,
"category": {
"name": "weight"
}
}
]
在 POST 服务中,我将数据输入到属性中,然后将其发送到服务器(简化示例)。新的对象数组应在下面的ratings 属性中声明
const scoreData= {
title: something,
ratings: score
};
return this.http.post(this.API_URL, scoreData);
编辑:所选解决方案确实有效!但可能过于复杂?为了比较,将features 对象的键和值属性迭代到新数组的方法是什么,例如:
const scoreData = {
title: "something",
ratings: [
{
"value": features.value, // Iterating through values here
"category": {
"name": features.key // ... and here
}
}
]
};
【问题讨论】:
标签: javascript arrays angular javascript-objects