我以处理系统不可接受的格式取回数据,并且试图转换数据。以下是我获取的数据以及下面的必需JSON。我尝试了其他操作,例如在数据中查找Object
并检查它是否包含多个元素,然后将该对象转换为Array[]
,但是我无法这样做。
如果您有任何意见,我将不胜感激。
if(typeof ob1=== "object" && Object.keys(ob1.length > 1) && typeof Object.keys(ob1) === "object" )
{
console.log(ob1); // I get all the objects and not the parent object i need to change.
}
当前数据:
ob1 : {id: 1, details: Object, profession: "Business"}
JSON:
{
"id": "1",
"details": {
"0": {
"name": "Peter",
"address": "Arizona",
"phone": 9900998899
},
"1": {
"name": "Jam",
"address": "Kentucky",
"phone": 56034033343
}
},
"profession": "Business"
}
所需数据:
{id: 1, details: Array[2], profession: "Business"}
必需的JSON:
{
"id": "1",
"details": [
{
"name": "Peter",
"address": "Arizona",
"phone": 9900998899
},
{
"name": "Jam",
"address": "Kentucky",
"phone": 56034033343
}
],
"profession": "Business"
}
最佳答案
您必须遍历details对象并将其转换为数组: