Yahoo! Developer Network Home - Help

YDN Site Search

# YUI Library Examples: YAHOO Global Object: Combining Simple Data Sets with `YAHOO.lang.merge`

# YAHOO Global Object: Combining Simple Data Sets with `YAHOO.lang.merge`

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

Click the Merge button to generate the merged set.

    set1 = { foo : "foo" };

    set2 = { foo : "BAR", bar : "bar"  };

    set3 = { foo : "FOO", baz : "BAZ" };

### result

    click Merge

## YAHOO.lang is packaged with the YAHOO Global Object

`YAHOO.lang` comes bundled with the YAHOO Global Object.

<script type="text/javascript" src="http://yui.yahooapis.com/2.5.1/build/yahoo/yahoo-min.js"></script>

If you are using any other YUI component on your page, you should already have `YAHOO.lang` available.

## Merging hash tables

In the example illustrated above, we merge three object literals in the form of hash tables. Note the key values in later parameters override those in previous parameters.

YAHOO.namespace('example'); YAHOO.example.set1 = { foo : "foo" }; YAHOO.example.set2 = { foo : "BAR", bar : "bar" }; YAHOO.example.set3 = { foo : "FOO", baz : "BAZ" }; YAHOO.example.doMerge = function () { var Ye = YAHOO.example; var merged = YAHOO.lang.merge(Ye.set1, Ye.set2, Ye.set3); // Output the stringified version of merged var result = YAHOO.util.Dom.get('demo_result'); result.innerHTML = YAHOO.example.stringifyObj(merged); } YAHOO.util.Event.on('demo_btn','click',YAHOO.example.doMerge);

### YUI Logger Output:

### YAHOO Global Object Examples:

- Type-Checking Your Data
- User Agent Detection
- Creating Class Hierarchies with `YAHOO.lang.extend`
- Creating a Composition-Based Class Structure Using `YAHOO.lang.augmentProto`
- Add Behavior to Objects or Static Classes with `YAHOO.lang.augmentObject`
- Combining Simple Data Sets with `YAHOO.lang.merge`

### More YAHOO Global Object Resources:

- User's Guide (external)
- API Documentation

Copyright © 2008 Yahoo! Inc. All rights reserved.

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