[英]How do I access the property of an object that is a string?
我有一個對象,需要將其從字符串轉換為before鈎子中的float:
{ '$near':
{ '$geometry': { type: 'Point', coordinates: [Array] },
'$minDistance': '0',
'$maxDistance': '10000'
}
}
如果它是字符串,如何訪問$ near屬性? 我需要將“坐標”內的值從字符串轉換為浮點數。
console.log(location['$near']);
不起作用
這應該工作:
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);
輸出:
Array [ 144.982, -37.864 ]
使用vanillaJS
可以訪問以下坐標
let location = { '$near':
{ '$geometry': { type: 'Point', coordinates: [{pointX: "1.23", pointY: "4.56"}] },
'$minDistance': '0',
'$maxDistance': '10000'
}
}
location['$near']['$geometry']['coordinates'] //[{"x":"1.23","y":"4.56"}]
location['$near']['$geometry']['coordinates'].forEach(cor =>{
console.log(cor.pointX);
console.log(cor.pointY)
})
location
通常保留給瀏覽器。 嘗試使用另一個變量名。 另外,對象中的鍵都是字符串,因此除了已使用的方法(鍵訪問器或點符號)以外,沒有其他特殊的訪問方法:
let _location = { '$near': { '$geometry': { type: 'Point', coordinates: [1,2] }, '$minDistance': '0', '$maxDistance': '10000' } } console.log(_location['$near']['$geometry']['coordinates']);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.