lib/field.filter.js
function getFieldsFromObj(data, fieldsToGet) {
    var newObj = {};
    _.each(fieldsToGet, function (fieldToGet) {

        // if the field to get is in the data, then just transfer it over and that is it
        if (data.hasOwnProperty(fieldToGet)) {
            newObj[fieldToGet] = data[fieldToGet];
            return true;
        }

        var fieldParts = fieldToGet.split('.');
        var len = fieldParts.length;
        var dataPointer = data;
        var tempPointer = newObj;
        var fieldPart, i;

        for (i = 0; i < len; i++) {
            fieldPart = fieldParts[i];

            // if doesn't exist, then break out of loop as there is no value in data for this
            if (typeof dataPointer[fieldPart] === 'undefined') {
                break;
            }
            // else we are at the end, so copy this value to the newObj
            else if (i === (len - 1)) {
                tempPointer[fieldPart] = dataPointer[fieldPart];
            }
            else {
                dataPointer = dataPointer[fieldPart];
                tempPointer = tempPointer[fieldPart] = tempPointer[fieldPart] || {};
            }
        }

        return true;
    });

    return newObj;
}
function getFieldsFromObj(data, fieldsToGet) {
    var newObj = {};
    _.each(fieldsToGet, function (fieldToGet) {

        // if the field to get is in the data, then just transfer it over and that is it
        if (data.hasOwnProperty(fieldToGet)) {
            newObj[fieldToGet] = data[fieldToGet];
            return true;
        }

        var fieldParts = fieldToGet.split('.');
        var len = fieldParts.length;
        var dataPointer = data;
        var tempPointer = newObj;
        var fieldPart, i;

        for (i = 0; i < len; i++) {
            fieldPart = fieldParts[i];

            // if doesn't exist, then break out of loop as there is no value in data for this
            if (typeof dataPointer[fieldPart] === 'undefined') {
                break;
            }
            // else we are at the end, so copy this value to the newObj
            else if (i === (len - 1)) {
                tempPointer[fieldPart] = dataPointer[fieldPart];
            }
            else {
                dataPointer = dataPointer[fieldPart];
                tempPointer = tempPointer[fieldPart] = tempPointer[fieldPart] || {};
            }
        }

        return true;
    });

    return newObj;
}
function getFieldsFromObj ( data , fieldsToGet ) { var newObj = { } ; _ . each ( fieldsToGet , function ( fieldToGet ) { // if the field to get is in the data, then just transfer it over and that is it if ( data . hasOwnProperty ( fieldToGet ) ) { newObj [ fieldToGet ] = data [ fieldToGet ] ; return true ; } var fieldParts = fieldToGet . split ( '.' ) ; var len = fieldParts . length ; var dataPointer = data ; var tempPointer = newObj ; var fieldPart , i ; for ( i = 0 ; i < len ; i ++ ) { fieldPart = fieldParts [ i ] ; // if doesn't exist, then break out of loop as there is no value in data for this if ( typeof dataPointer [ fieldPart ] === 'undefined' ) { break ; } // else we are at the end, so copy this value to the newObj else if ( i === ( len - 1 ) ) { tempPointer [ fieldPart ] = dataPointer [ fieldPart ] ; } else { dataPointer = dataPointer [ fieldPart ] ; tempPointer = tempPointer [ fieldPart ] = tempPointer [ fieldPart ] || { } ; } } return true ; } ) ; return newObj ; }
Get all fields specified from an object
@param data
@param fieldsToGet An array of fields where each field is dot notation
@returns {{}}
Get all fields specified from an object