# MERGE\_OBJECTS

**The function MERGE\_OBJECTS combines objects by adding the properties of additional objects to the object that resulted from merging earlier objects.**

This function can take input in two forms: either an arbitrary number of objects or an arbitrarily long list of objects. It outputs a single object, with the properties of the given objects combined. These properties are merged by adding the properties of additional objects to the combination of the previous objects; that is, the resulting object of the MERGE\_OBJECT function will have every name-value pair that appears within the last object given, even if that requires the value associated with a previously added name-value pair to be overwritten.&#x20;

#### Declaration

```javascript
MERGE_OBJECTS(object_1, object_2,...object_n) -> combined_object
```

```javascript
MERGE_OBJECTS(object_list) -> combined_object
```

&#x20;

#### Parameters

**object\_n**(type: _object_) The _nth_ object to be merged. There is no upper limit on _n_; MERGE\_OBJECTS can combine any number of objects.

**object\_list** (type: _list_) A list of objects to merge. There is no upper limit to the number of items that can occupy this list; MERGE\_OBJECTS can combine any number of objects.

#### Return Values

**combined\_object** (type: _object_) The object created by combining the properties of all the given objects, whether they were given as individual objects or in a list of objects. If any of the input objects share property names, the name-value pair in the **combined\_object** will have the same value as the last given object with that property name.

#### Examples

The following example merges the objects {"first\_name": "John"} and {"last\_name": "Smith"} into a single object containing both the name-value pair in the first object and the name-value pair in the second object:

```javascript
MERGE_OBJECTS({"first_name": "John"}, {"last_name": "Smith"}) -> 
{  
 "first_name": "John",  
 "last_name": "Smith"  
}
```

If the input objects contain only unique property names, such as in the above example, the resulting object contains every name-value pair from within every input object. However, if the input objects share property names, the value given by later objects will overwrite the values given by earlier objects. The following example merges the objects {"first\_name": "John"} and {"first\_name": "Suzy"} into a single object. Note that the resulting object has "Suzy" as the value of "first\_name", because the object {"first\_name": "Suzy"} was given after the object {"first\_name": "John"}:

```javascript
MERGE_OBJECTS({"first_name": "John"}, {"first_name": "Suzy"}) -> 
{  
 "first_name": "Suzy"  
}
```

Switching the order that these input values are given changes which value overwrites the other. The following example merges the objects {"first\_name": "Suzy"} and {"first\_name": "John"} into a single object. Note that the resulting object has "John" as the value of "first\_name", because the object {"first\_name": "John"} was given after the object {"first\_name": "Suzy"}:

```javascript
MERGE_OBJECTS({"first_name": "Suzy"}, {"first_name": "John"}) -> 
{  
 "first_name": "John"  
}
```

MERGE\_OBJECTS can be used to combine objects with any number of name-value pairs, and these objects do not need to be hardcoded. Assume the following examples have access to the following variables:

```javascript
object1 = {  
 "first_name": "Alice",  
 "number": 1,  
 "apples": TRUE  
}  
object2 = {  
"first_name": "Bob",  
"number": 2,  
"bananas": FALSE  
}  
object3 = {  
"first_name": "Carol",  
"number": "three",  
"cucumbers": TRUE  
}
```

The following example merges the **object1** and **object2** into a single object. Note that the resulting object has "Bob" as the value of "first\_name" and 1 as the value of "number", because the **object2** was given after **object1**:

```javascript
MERGE_OBJECTS(object1, object2) -> {  
 "first_name": "Bob",   
 "number": 2,   
 "apples": TRUE,   
 "bananas": FALSE  
}
```

When objects share a property name and one value overwrites another, it makes no difference if these values are not the same type. The above example merged **object1** and **object2**, both of which stored number values under the name "number." **object3** stores a string value under the name "number", and the following example demonstrates how MERGE\_OBJECTS combines **object1**, **object2**, and **object3**. Note that, because **object3** was given last, the value associated with "number" is "three"  – there is no indication that **object1** and **object 2** stored numbers under the same name.

```javascript
MERGE_OBJECTS(object1, object2, object3) -> 
{  
 "first_name": "Carol",   
 "number": "three",   
 "apples": TRUE,   
 "bananas": FALSE,   
 "cucumbers": TRUE  
}
```

For the sake of clarity, the above examples took multiple objects as input. However, this does not have to be the case; MERGE\_OBJECTS also accepts lists of objects as input. For the next example, assume the formula has access to the following list:

```javascript
example_list = [object1, object2, object3]
```

Because **example\_list** lists **object1**, **object2**, and **object3** in the same order that they were given as input in the above example, using **example\_list** as input in MERGE\_OBJECTS results in the same output:

```javascript
MERGE_OBJECTS(example_list) -> {  
"first_name": "Carol",   
"number": "three",   
"apples": TRUE,   
"bananas": FALSE,   
"cucumbers": TRUE  
}
```

The MERGE\_OBJECTS function can take input in two forms: any number of objects, or a list of objects. It cannot take both at the same time; that is, given a list of objects _and_ another object, it cannot combine all of the given objects the way objects were merged in the above examples. In order for MERGE\_OBJECTS to combine the objects given a list with a separate object, MERGE\_OBJECTS functions will need to be nested. To demonstrate, assume the last formula has access to the following list:

```javascript
example_list2 = [object1, object2]
```

The following example merges the objects in **example\_list2** – **object1** and **object2**, in that order –  and then merges the resulting object with **object3**. Note that, because the objects were merged in the same order as in the above examples, the output is the same:

```javascript
MERGE_OBJECTS(MERGE_OBJECTS(example_list2), object3) -> 
{  
"first_name": "Carol",   
"number": "three",   
"apples": TRUE,   
"bananas": FALSE,   
"cucumbers": TRUE  
}
```
