back to Overview

_.copyObj()

Copies every property of the first object into the second object.
Util module only.

Syntax Variants

_.copyObj(from)
_.copyObj(from, to)

Parameters

from
the object to copy from
to (optional)
the object to copy to. If not given, a new object will be created.
(return value)
the object that has been copied to

Description

Copies every property of the first object into the second object. The properties are copied as shallow-copies.

Example

Copying properties:

var target = {a:3, c: 3};
_.copyObj({a: 1, b: 2}, target); // target is now {a: 1, b: 2, c: 3}

Example

Inline property merge:

var target = _.copyObj({a: 1, b: 2}, {a:3, c: 3}); // target is now {a: 1, b: 2, c: 3}

Example

Duplicating an object:

var target = _.copyObj({a: 1, b: 2}); // target is now {a: 1, b: 2}

See also..

  • _.extend() is very similar to copyObj(), but with a slightly different syntax.
  • _.merge() copies a list of objects into a new object.

Comments

comments powered by Disqus

back to Overview

Functions