util.extend(receiver, contributor)
The content in this help topic pertains to SuiteScript 2.0.
Method Description |
Copies the properties in a source object to a destination object. You can use this method to merge two objects. |
Returns |
The destination object. |
Supported Script Types |
Client and server scripts For more information, see SuiteScript 2.x Script Types. |
Governance |
None |
Module |
|
Since |
2016.1 |
Syntax
The following code snippets shows the syntax for this member. It is not a functional example. For a full script sample, see N/util Module Script Sample.
This snippet shows combining two objects without the same keys:
//Add additional code
...
var colors = {};
var firstSet = {'color1':'red',
'color2':'yellow',
'color3':'blue'};
var secondSet = {'color4':'green',
'color5':'orange',
'color6':'violet'
};
// Extends colors object with the information in firstSet
// Colors will get {'color1':'red','color2':'yellow','color3':'blue'}
util.extend(colors, firstSet);
// Extends colors object with the information in secondSet
// Colors will get {'color1':'red','color2':'yellow','color3':'blue','color4':'green','color5':'orange','color6':'violet'}
util.extend(colors, secondSet);
});
...
//Add additional code
The following snippet shows overriding two objects with a few similar keys:
//Add additional code
...
var colors = {};
var firstSet = {'color1':'red',
'color2':'yellow',
'color3':'blue'
};
var secondSet = {'color2':'green',
'color3':'orange',
'color4':'violet'
};
// Extends colors object with the information in firstSet
// Colors will get {'color1':'red','color2':'yellow','color3':'blue'}
util.extend(colors, firstSet);
// Extends colors object with the information in secondSet and overrides the value if there are similar keys
// Colors will get {'color1':'red','color2':'green','color3':'orange','color4':'violet'}
util.extend(colors, secondSet);
var x = 0;
});
...
//Add additional code