Singularize arrays (named array elements) - Node.js Array

Node.js examples for Array:Array Value

Description

Singularize arrays (named array elements)

Demo Code

exports.singularizeArrays = function(miObject) {
    return JSON.parse(JSON.stringify(miObject, function(key, value) {
        if (value && (Array.isArray(value))) {
            var replacement = [];
            for (var k in value) {
                if (Object.hasOwnProperty.call(value, k)) {
                    var singular = (key) ? singularize(key) : "item";
                    var m = {};
                    m[singular] = value[k];
                    replacement[k] = m;//  ww w .j av a  2s  .com
                }
            }
            return replacement;
        }
        return value;
    }));
};

Related Tutorials