Search code examples
javascriptfeathersjs

How do I access the property of an object that is a string?


I have an object which I need to convert from a string to a float in a before hook:

{ '$near': 
   { '$geometry': { type: 'Point', coordinates: [Array] },
     '$minDistance': '0',
     '$maxDistance': '10000'
   }
}

How do I access the $near property if its a string? I need to convert the values inside 'coordinates' from a string to a float.

console.log(location['$near']);

Doesn't work


Solution

  • This should work:

    let thelocation = { '$near': 
       { '$geometry': { type: 'Point', coordinates:  [ "144.982", "-37.864" ] },
         '$minDistance': '0',
         '$maxDistance': '10000'
       }
    };
    let coords = [parseFloat(thelocation.$near.$geometry.coordinates[0]),
                  parseFloat(thelocation.$near.$geometry.coordinates[1])];
    console.log(coords);
    

    Output:

    Array [ 144.982, -37.864 ]