0

I have a dynamically loaded object for which the print of JSON.stringify() in the console looks like this:

var data = [
    {
        "attributes": {
            "OBJECTID": 1046
        },
        "geometry": {
            "x": -9814734.1764,
            "y": 5130578.545900002
        }
    },
    {
        "attributes": {
            "OBJECTID": 1051
        },
        "geometry": {
            "x": -9814335.3286,
            "y": 5130497.9344
        }
    },
    {
        "attributes": {
            "OBJECTID": 1052
        },
        "geometry": {
            "x": -9814639.1784,
            "y": 5130583.1822
        }
    },
    {
        "attributes": {
            "OBJECTID": 1053
        },
        "geometry": {
            "x": -9814496.7964,
            "y": 5130560.822300002
        }
    }
];

How can I apply .toFixed(2) function to each of X and Y in geometry nodes on the fly?

halfer
  • 19,824
  • 17
  • 99
  • 186
Mona Coder
  • 6,212
  • 18
  • 66
  • 128
  • 1
    Possible duplicate of [Modify object property in an array of objects](https://stackoverflow.com/questions/16691833/modify-object-property-in-an-array-of-objects) – Rajesh Nov 30 '17 at 07:01

4 Answers4

2

You need to use the map function:

const formattedData = data.map(d => {
  d.geometry.x = parseFloat(d.geometry.x).toFixed(2);
  d.geometry.y = parseFloat(d.geometry.y).toFixed(2);

  return d;
})

Feel free to change the property name (x -> xFormatted) if you don't wish to override the original data.

klugjo
  • 19,422
  • 8
  • 57
  • 75
1

Use map

data = data.map( function(s){
  s.geometry.x = s.geometry.x.toFixed(2);
  s.geometry.y = s.geometry.y.toFixed(2);
  return s;
})

Edit

Or with forEach

data.forEach( function(s){
  s.geometry.x = s.geometry.x.toFixed(2);
  s.geometry.y = s.geometry.y.toFixed(2);
})
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • 1
    I do not think we need `.map` here. `s` is an object, so it will mutate original object in array anyway. – Rajesh Nov 30 '17 at 07:00
0

Try this

//Your Data
var data = [
    {
        "attributes": {
            "OBJECTID": 1046
        },
        "geometry": {
            "x": -9814734.1764,
            "y": 5130578.545900002
        }
    },
    {
        "attributes": {
            "OBJECTID": 1051
        },
        "geometry": {
            "x": -9814335.3286,
            "y": 5130497.9344
        }
    },
    {
        "attributes": {
            "OBJECTID": 1052
        },
        "geometry": {
            "x": -9814639.1784,
            "y": 5130583.1822
        }
    },
    {
        "attributes": {
            "OBJECTID": 1053
        },
        "geometry": {
            "x": -9814496.7964,
            "y": 5130560.822300002
        }
    }
];


//Updating data 
data.forEach(function(item,index){
  item["geometry"]["x"]=item["geometry"]["x"].toFixed(2);
  item["geometry"]["y"]=item["geometry"]["y"].toFixed(2);
})

console.log(JSON.stringify(data))
Sourabh Somani
  • 2,138
  • 1
  • 13
  • 27
0

Just iterate using the forEach function. Also make sure to parse the value using parseFloat before calling the toFixed function.

data.forEach(entry => {
    entry.geometry.x = parseFloat(entry.geometry.x).toFixed(2);
    entry.geometry.y = parseFloat(entry.geometry.y).toFixed(2);
});
Daniel Pucher
  • 179
  • 1
  • 5