YUI 3.x Home -

YUI Library Examples: DataSchema Utility: DataSchema.Array

DataSchema Utility: DataSchema.Array

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

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

  1. YUI().use("dataschema-array", function(Y) {
  2. var data_in = [
  3. {make:"Chevrolet",model:"Bel Air",year:1957},
  4. {make:"Dodge",model:"Dart",year:1964},
  5. {make:"Ford",model:"Mustang",year:1968}
  6. ],
  7. schema = {
  8. // Or simply: ["make", "model", "year"]
  9. resultFields: [{key:"make"}, {key:"model"}, {key:"year"}]
  10. },
  11. data_out = Y.DataSchema.Array.apply(schema, data_in));
  12.  
  13. alert(data_out);
  14. });
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 = {
            // Or simply: ["make", "model", "year"]
            resultFields: [{key:"make"}, {key:"model"}, {key:"year"}]
        },
        data_out = Y.DataSchema.Array.apply(schema, data_in));
 
    alert(data_out);
});

...JavaScript arrays

  1. YUI().use("dataschema-array", function(Y) {
  2. var data_in = [
  3. ["Chevrolet", "Bel Air", 1957],
  4. ["Dodge", "Dart", 1964],
  5. ["Ford", "Mustang", 1968]
  6. ],
  7. schema = {
  8. // Or simply: ["make", "model", "year"]
  9. resultFields: [{key:"make"}, {key:"model"}, {key:"year"}]
  10. },
  11. data_out = Y.DataSchema.Array.apply(schema, data_in));
  12.  
  13. alert(data_out);
  14. });
YUI().use("dataschema-array", function(Y) {
    var data_in = [
            ["Chevrolet", "Bel Air", 1957],
            ["Dodge", "Dart", 1964],
            ["Ford", "Mustang", 1968]
        ],
        schema = {
            // Or simply: ["make", "model", "year"]
            resultFields: [{key:"make"}, {key:"model"}, {key:"year"}]
        },
        data_out = Y.DataSchema.Array.apply(schema, data_in));
 
    alert(data_out);
});

...primitive data types

  1. YUI().use("dataschema-array", function(Y) {
  2. var data_in = [
  3. "1957 Chevrolet Bel Air", "1964 Dodge Dart", "1968 Ford Mustang"
  4. ],
  5. schema = {
  6. // Or simply: ["make", "model", "year"]
  7. resultFields: [{key:"make"}, {key:"model"}, {key:"year"}]
  8. },
  9. data_out = Y.DataSchema.Array.apply(schema, data_in));
  10.  
  11. alert(data_out);
  12. });
YUI().use("dataschema-array", function(Y) {
    var data_in = [
            "1957 Chevrolet Bel Air", "1964 Dodge Dart", "1968 Ford Mustang"
        ],
        schema = {
            // Or simply: ["make", "model", "year"]
            resultFields: [{key:"make"}, {key:"model"}, {key:"year"}]
        },
        data_out = Y.DataSchema.Array.apply(schema, data_in));
 
    alert(data_out);
});

...or even a combination.

...primitive data types

  1. YUI().use("dataschema-array", function(Y) {
  2. var data_in = [
  3. {make:"Chevrolet",model:"Bel Air",year:1957},
  4. ["Dodge", "Dart", 1964],
  5. "1968 Ford Mustang"
  6. ],
  7. schema = {
  8. // Or simply: ["make", "model", "year"]
  9. resultFields: [{key:"make"}, {key:"model"}, {key:"year"}]
  10. },
  11. data_out = Y.DataSchema.Array.apply(schema, data_in));
  12.  
  13. alert(data_out);
  14. });
YUI().use("dataschema-array", function(Y) {
    var data_in = [
            {make:"Chevrolet",model:"Bel Air",year:1957},
            ["Dodge", "Dart", 1964],
            "1968 Ford Mustang"
        ],
        schema = {
            // Or simply: ["make", "model", "year"]
            resultFields: [{key:"make"}, {key:"model"}, {key:"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