-2

I'm trying to convert object from array of object in JavaScript but not working

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

from that I want only this object

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

I have tried like that to convert but not working

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);
  • 1
    parsedValue is an array. you should not be reading key-value of the same. just read the object with index of the array element – Chiranjib Jul 15 '19 at 06:41
  • i'm getting that json values from api and i have convert it to object like [{1,2,3}] to {1,2,3} – Dharshan PLUV Jul 15 '19 at 07:11

2 Answers2

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

Get the object:

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

console.log(result)

Output:

{type: "D", xcor: 86.54545593261719, ycor: 413.4545593261719}
MH2K9
  • 11,951
  • 7
  • 32
  • 49
0

Try this way

   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);
Janaki Narayanan
  • 523
  • 6
  • 24