YUI 3.x Home -

Site Search (YDN & YUIBlog):

# YUI Library Examples: DataSchema Utility: DataSchema.Array

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

## DataSchema Utility: DataSchema.Array

DataSchema.Array normalizes arbitrary JavaScript array data against a given schema into an object with known properties.

View example in new window.

#### Array of objects

###### Data

    [
        {make:"Chevrolet", model:"Bel Air", year:1957},
        {make:"Dodge", model:"Dart", year:1964},
        {make:"Ford", model:"Mustang", year:1968}
    ]

###### Schema

    {
        resultFields: [{key:"make"}, {key:"model"}, {key:"year"}]
    }

###### Normalized data

#### Array of arrays

###### Data

    [
        ["Chevrolet", "Bel Air", 1957],
        ["Dodge", "Dart", 1964],
        ["Ford", "Mustang", 1968]
    ]

###### Schema

    {
        resultFields: [{key:"make"}, {key:"model"}, {key:"year"}]
    }

###### Normalized data

#### Simple array of primitives

###### Data

    [
        "1957 Chevrolet Bel Air", "1964 Dodge Dart", "1968 Ford Mustang"
    ]

###### Schema

    {
        resultFields: [{key:"make"}, {key:"model"}, {key:"year"}]
    }

###### Normalized data

In order to use DataSchema.Array, input data must be a JavaScript array. Within that array, you may have...

...JavaScript objects

YUI().use("dataschema-array", function(Y) { var data_in = [ {make:"Chevrolet",model:"Bel Air",year:1957}, {make:"Dodge",model:"Dart",year:1964}, {make:"Ford",model:"Mustang",year:1968} ], schema = { resultFields: [{key:"make"}, {key:"model"}, {key:"year"}] // Or simply: ["make", "model", "year"] }, data_out = Y.DataSchema.Array.apply(schema, data_in)); alert(data_out); });

...JavaScript arrays

YUI().use("dataschema-array", function(Y) { var data_in = [ ["Chevrolet", "Bel Air", 1957], ["Dodge", "Dart", 1964], ["Ford", "Mustang", 1968] ], schema = { resultFields: [{key:"make"}, {key:"model"}, {key:"year"}] // Or simply: ["make", "model", "year"] }, data_out = Y.DataSchema.Array.apply(schema, data_in)); alert(data_out); });

...primitive data types

YUI().use("dataschema-array", function(Y) { var data_in = [ "1957 Chevrolet Bel Air", "1964 Dodge Dart", "1968 Ford Mustang" ], schema = { resultFields: [{key:"make"}, {key:"model"}, {key:"year"}] // Or simply: ["make", "model", "year"] }, data_out = Y.DataSchema.Array.apply(schema, data_in)); alert(data_out); });

...or even a combination.

YUI().use("dataschema-array", function(Y) { var data_in = [ {make:"Chevrolet",model:"Bel Air",year:1957}, ["Dodge", "Dart", 1964], "1968 Ford Mustang" ], schema = { resultFields: [{key:"make"}, {key:"model"}, {key:"year"}] // Or simply: ["make", "model", "year"] }, data_out = Y.DataSchema.Array.apply(schema, data_in)); alert(data_out); });

Copyright © 2009 Yahoo! Inc. All rights reserved.

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