Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

In this Page

...

This function is not supported in Spark pipelines.

The extend object method can also be used to convert an array of objects into an object. An example illustrating the same is described in the Example Use Cases section below.

Description

Returns a new object with the properties of the current one merged with the properties of the objects that were passed in. This is similar to http://underscorejs.org/#extend, but the extend object method returns a new object instead of modifying the given one. 

Note
Syntax


Code Block
object.extend(target:data)


Example

Expression: $.extend({ newField1 : 'foo' }, { newField2 : 'bar' })

Result:

Code Block
{
      "newField1": "foo",
      "newField2": "bar"
}


Note

When using the above expression in a Mapper ensure that the input stream is not empty else a null value error will be shown. If an empty document is to be created then use {} instead of $:

{}.extend({ newField1 : 'foo' },{ newField2 : 'bar' })


...

Expand
titleExtend Method: Converting an array into an object

A JSON Array of objects with unique/non-overlapping keys can be converted to an object using the extend object method along with a Spread Operator.  The extend object method does so by creating objects dynamically from other objects passed in as arguments, the spread operator will have to be used to indicate that the elements of the array should be treated as the arguments to the function. The expression to be used is:


Paste code macro
titleExpression using extend method to convert an array into an object
{}extend(...$<Array_name>)

1. Input the array into Mapper Snap (this can be done using a JSON Generator Snap). For this example, the following array will be used:

Paste code macro
languagejson
titleJSON Array
{
 "myArray": [
   {
     "Header1": {
       "a": 11,
       "b": 21,
       "c": 31
     }
   },
   {
     "Header2": {
       "a": 12,
       "b": 22,
       "c": 32
     }
   },
   {
     "Header3": {
       "a": 13,
       "b": 23,
       "c": 33
     }
   }
 ]
}

2. The Mapper Snap must be configured as shown below. Notice the Expression and Target path fields.

3. When executed, the output will be an object.

Note

The array should have unique keys for this method to work, if they overlap then the last element of the array will be converted.