繁体   English   中英

javascript-无法使用其键访问对象的属性?

[英]javascript - can't access property of an object using its key?

在Google Earth编辑器中,我们使用reduceRegion()函数创建了一个对象:

var meanValue2015 = ndvi2015.reduceRegion({
  reducer: ee.Reducer.mean(),
  geometry: justEC15.geometry(),
  crs: 'EPSG:4326',
  scale: 30,
});

我的问题是,meanValue2015似乎是一个对象-我输入

print(meanValue2015);
print(typeof(meanValue2015));
print(Object.keys(meanValue2015));

并分别获得“对象(1属性)-NDVI:0.3177 ...”,“对象”,然后分别是“ []”? 接着

print(meanValue2015.NDVI);
print(meanValue2015['NDVI']);

是不确定的? 我在这里做错什么明显吗?

meanValue2015是Earth Engine对象:Dictionary。 所以..

var meanValue2015 = ndvi2015.reduceRegion({
  reducer: ee.Reducer.mean(),
  geometry: justEC15.geometry(),
  crs: 'EPSG:4326',
  scale: 30,
});

var realValue = ee.Number(meanValue2015.get("ndvi"));
print("real value (object):", realValue)

// realValue is a EE object, so you have to use it as is..
// this would be easy, but it is wrong..
var newValueWrong = realValue + 1

// this is the right way..
var newValueRight = realValue.add(ee.Number(1))

print("wrong:", newValueWrong)
print("right:", newValueRight)

// there is another way, but it is not recommendable because you'd
// be wasting EE computing capacity (Google's gift! don't waste it)

var localValue = realValue.getInfo() + 1

print("value in the local scope (try to avoid)", localValue)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM