我正在使用辅助函数turf.point()
const feature = turfHelpers.point(coords, properties, { id: properties.id });
properties
看起来像这样
properties = {
id: 1,
thisWorks: 'no problem'
foo: {
thisDoesntWork: 'this is a problem'
}
}
当我用turfHelpers.point()
创建feature
时,它会弄乱对象。嵌套对象不再是object
,而是字符串化...
所以,features.properties
是
{
id: 1,
thisWorks: 'no problem'
foo: "{
thisDoesntWork: 'this is a problem'
}"
}
现在,我无法访问。 feature.properties.foo.thisDoesntWork
了,因为它是一根绳子... turf.js
为什么要这样做?
让我们以可运行的形式提出问题。
const turfHelpers = turf.helpers;
const coords = [100, 14];
const properties = {
id: 1,
thisWorks: 'no problem',
foo: {
thisDoesntWork: 'this is a problem'
}
};
var feature1 = turfHelpers.point(coords, properties, {
id: properties.id
});
// Print out on console
console.log(feature1.properties); //look OK
console.log(feature1.properties.foo); //also OK
console.log(feature1.properties.foo.thisDoesntWork); //also OK
<script src="https://cdnjs.cloudflare.com/ajax/libs/Turf.js/5.1.5/turf.min.js"></script>
然后,希望它有助于导致解决方案的讨论。