YUI 3.x Home -

YUI Library Examples: The YUI Global Object: Combine Data Sets with merge

Note: This is YUI 3.x. Looking for YUI 2.x?

The YUI Global Object: Combine Data Sets with merge

merge allows you to combine any number of input objects and output a single merged set.

If the input is a single object, the return value will be a shallow copy of the input, meaning that the return value is a different object, but any non-primitive value in the input object is referenced in the returned object.

Click the "Merge" button to generate the merged set, the "Copy" button to create a shallow copy of an object.

set1 = { foo : "foo" };
set2 = { foo : "BAR", bar : "bar"  };
set3 = { foo : "FOO", baz : "BAZ" };

result

click Merge or Copy

Using merge

Instantiate YUI

Merging hash tables

When the "Merge" button is clicked, we merge three object literals in the form of hash tables. Note the key values in later parameters override those in previous parameters.

Creating Shallow Copies

When the "Copy" button is clicked, we create use merge on a single object in order to create a shallow clone. The code illustrates the fact that object properties of the result object are shared with the input object.

See the clone method to create deep copies of objects.

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings