-2

I have this

Object { name: "Fresh", styleUrl: "#icon-959-F8971B-nodesc", ExtendedData: "", Point: Object }

and there is another property in the last one (Point:Object) like

coordinate:"1,2,3"

I want to access to the string "1,2,3" and separate it to variable like

x=1,y=2

I don't need 3 .Do you have any idea for javascript?

Mona
  • 1
  • 3

2 Answers2

1

You can access the coordinate string via dot-notation like so:

var theObject; // your Object { name: "Fresh", styleUrl: "#icon-959-F8971B-nodesc", ExtendedData: "", Point: Object }
var coordinate = theObject.Point.coordinate;

and if you want to convert it from a String into an object with x and y properties you can split it into an Array and extract the values like so:

var points = coordinate.split(',');
var x = points[0];
var y = points[1];

var coordinatePoint = {
    x: x,
    y: y
};

and access the x/y values like so:

console.log(coordinatePoint.x);
Alex McMillan
  • 17,096
  • 12
  • 55
  • 88
0

I does not matter how many nested objects there are, you can just access them normally with the . notation or with [].

For example with your data:

var obj = { name: "Fresh", styleUrl: "#icon-959-F8971B-nodesc", ExtendedData: "", Point: {"coordinate":"1,2,3"} };

var coord = obj.Point.coordinate;

var array = coord.split(',');

// for example, get the second value, at index 1 since we start from 0
console.log(array[1]);

prints to the console:

2
Roope
  • 4,469
  • 2
  • 27
  • 51