尝试在JavaScript中将对象数组转换为对象

时间:2019-07-15 06:39:52

标签: javascript

我正在尝试从JavaScript中的对象数组转换对象,但无法正常工作

[{"external_parts":"[ { "type": "D","xcor": 86.54545593261719,"ycor": 413.4545593261719} ] "}], 

我只想要这个对象

{ "type": "D","xcor": 86.54545593261719,"ycor": 413.4545593261719}

我试图那样转换但不能正常工作

var parsedValue = responseData.data.items[0].external_parts;
//convert
var result = {};
for (var i=0; i<parsedValue.length; i++) {
  result[parsedValue[i].key] = parsedValue[i].value;
}
coordinatesArray.push(parsedValue);

2 个答案:

答案 0 :(得分:0)

const data = [
    {
        "external_parts":[
            { 
                "type": "D",
                "xcor": 86.54545593261719,
                "ycor": 413.4545593261719
            }
        ]
    }
];

获取对象:

var result = data[0].external_parts[0]

console.log(result)

输出:

{type: "D", xcor: 86.54545593261719, ycor: 413.4545593261719}

答案 1 :(得分:0)

尝试这种方式

   var parsedValue = responseData.data.items[0].external_parts;
    //convert
    var result;
    for (var i=0; i<parsedValue.length; i++) {
     result=responseData.data.items[0].external_parts[i]
    }
    coordinatesArray.push(result);