GEE: data structure - Object object (creation, access, modification, deletion, traversal)

In the Google Earth Engine (GEE) platform, "Object" refers to a data structure, which is composed of a set of properties (key-value pairs) (key-value), which can be numbers, strings, Boolean values, array or other object.

In GEE, many data are stored in the form of objects, such as images, geospatial vector data, time series, etc. Objects can be created, manipulated and analyzed using the JavaScript programming language.

In GEE, objects are typically represented using JavaScript Object Notation (JSON), a lightweight data-interchange format. Properties within an object can be accessed and manipulated using a JavaScript object-like syntax.

This article introduces the code and examples for creating, accessing attributes, modifying, deleting, and traversing Object data structures on the GEE platform



In Google Earth Engine, JavaScript programming language can be used to operate on Object. Here are some common ways to do it:

1. Create Object

In Google Earth Engine, you can use JavaScript's object literal (Object Literal) syntax to create an object. The specific operation method is as follows:

// 创建一个对象
var myObject = {
    
    
  key1: 'value1',
  key2: 'value2',
  key3: 'value3'
};

// 输出对象的属性
print(myObject);

In the above example, we use the object literal syntax to create an object myObject, which contains three key-value pairs. The key names of the object are key1, key2 and key3, and the corresponding key values ​​are value1, value2 and value3 respectively. Finally, we output the entire object using the print() function.

It should be noted that in Google Earth Engine, objects can not only be ordinary objects in JavaScript, but also various objects such as Image, Feature, Geometry and so on provided by Earth Engine. For example, you can create an Image object with the ee.Image() method, a Feature object with the ee.Feature() method, and so on.

2. Access Object properties

In Google Earth Engine, you can use JavaScript's point syntax (Dot Notation) or square bracket syntax (Bracket Notation) to access the properties of an object. The specific operation method is as follows:

// 创建一个对象
var myObject = {
    
    
  key1: 'value1',
  key2: 'value2',
  key3: 'value3'
};

// 使用点语法访问对象属性
var value1 = myObject.key1;

// 使用方括号语法访问对象属性
var value2 = myObject['key2'];

// 输出属性值
print('value1:', value1);
print('value2:', value2);

In the above example, we first created an object myObject, which contains three key-value pairs. We then access the object's properties using dot syntax and square bracket syntax, obtaining the values ​​of the key1 and key2 properties, respectively. Finally, we output the values ​​of these two properties using the print() function.

It should be noted that the difference between the dot syntax and the square bracket syntax is that the dot syntax needs to use the attribute name as the identifier, while the square bracket syntax needs to use the string form of the attribute name as the index. For example, in this example, both myObject.key1 and myObject['key1'] can access the value of the key1 property, but if the property name contains special characters or spaces, etc., it can only be accessed using the square bracket syntax.

3. Modify Object properties

In Google Earth Engine, you can use JavaScript's point syntax (Dot Notation) or square bracket syntax (Bracket Notation) to modify the properties of an object. The specific operation method is as follows:

// 创建一个对象
var myObject = {
    
    
  key1: 'value1',
  key2: 'value2',
  key3: 'value3'
};

// 修改对象属性的值
myObject.key1 = 'new value1';
myObject['key2'] = 'new value2';

// 输出修改后的属性值
print(myObject);

In the above example, we first created an object myObject, which contains three key-value pairs. We then modified the values ​​of the key1 and key2 attributes using dot syntax and square bracket syntax. Finally, we output the entire modified object using the print() function.

It should be noted that the difference between the dot syntax and the square bracket syntax is that the dot syntax needs to use the attribute name as the identifier, while the square bracket syntax needs to use the string form of the attribute name as the index. There is a similar difference when modifying attribute values. At the same time, you need to pay attention to whether the properties of the object can be modified. Some object properties are read-only and cannot be modified. For example, the properties of the Image object are read-only.

4. Delete the Object property

In Google Earth Engine, JavaScript's delete keyword can be used to delete properties of an object. The specific operation method is as follows:

// 创建一个对象
var myObject = {
    
    
  key1: 'value1',
  key2: 'value2',
  key3: 'value3'
};

// 删除对象的属性
delete myObject.key1;
delete myObject['key2'];

// 输出删除后的对象
print(myObject);

In the above example, we first created an object myObject, which contains three key-value pairs. Then, we deleted the key1 and key2 attributes using the delete keyword. Finally, we use the print() function to output the entire object after deletion.

It is important to note that when deleting an object property, if the property does not exist, nothing will happen. At the same time, some object properties are read-only and cannot be deleted. For example, the properties of the Image object are read-only.

5. Traversing Object properties

In Google Earth Engine, you can use the for…in loop to iterate over all properties of an object. The specific operation method is as follows:

// 创建一个对象
var myObject = {
    
    
  key1: 'value1',
  key2: 'value2',
  key3: 'value3'
};

// 遍历对象的属性
for (var key in myObject) {
    
    
  print(key + ': ' + myObject[key]);
}
// 遍历对象的属性
for (var key in myObject) {
    
    
  if (myObject.hasOwnProperty(key)) {
    
    
    var value = myObject[key];
    // 处理属性和值
  }
}

In the above example, we first created an object myObject, which contains three key-value pairs. Then, we use the for...in loop statement to traverse all properties of the object, and use the print() function to output the name and corresponding value of each property.

It should be noted that the for...in loop statement traverses the enumerable properties of the object, not including the properties on the prototype chain. If you need to traverse all properties on the object's prototype chain, you can use the Object.getOwnPropertyNames() method.

These manipulation methods are the basic methods of using JavaScript objects, and can also be used to manipulate Objects in Google Earth Engine. Note that some properties of GEE objects are read-only and cannot be modified directly.

6. Get all keys of Object

In Google Earth Engine, you can use JavaScript's Object.keys() method to get all the keys of an object (key). The specific operation method is as follows:
insert image description here

// 创建一个示例对象
var myObject = {
    
    
  key1: 'value1',
  key2: 'value2',
  key3: 'value3'
};

// 使用 Object.keys() 方法获取对象的键名
var keys = Object.keys(myObject);

// 输出所有的键名
print('Object keys:', keys);

In the above example, we first created a sample object myObject, which contains three key-value pairs. Then, we use the Object.keys() method to get all the keys of the object and store them in the variable keys. Finally, we use the print() function to output the keys array, which is all the keys of the object.

It should be noted that the Object.keys() method returns an array, and the elements in the array are all the keys of the object. If you need to get the key value of the object, you can use a method like myObject[key] to access, where key is a key name of the object.

Guess you like

Origin blog.csdn.net/qq_35591253/article/details/130020826