Quickly filter an object by keys I am trying to optimize an object filter function. Given an array of keys, I need to filter an object. I feel that creating a new object each time may degrade performance. Is there a way to mock the Array.prototype.filter function for objects?



function main() {
    var Types = {
        INTEGER:   { value: 'int'    },
        CHARACTER: { value: 'char'   },
        FLOAT:     { value: 'float'  },
        DOUBLE :   { value: 'double' },
        STRING:    { value: 'str'    },
        BOOLEAN:   { value: 'bool'   }
    };
    
    var Numbers = filterByKeys(Types, [ 'INTEGER', 'FLOAT', 'DOUBLE' ]);
    
    console.log(Numbers);
};

function listToSet(list) {
    var _set = {};
    if (Ext.isArray(list)) {
        for (var i = 0; i &lt; list.length; i++) {
            _set[list[i]] = true;
        }
    }
    return _set;
};

function filterByKeys(obj, keep) {
    var result = {},
        unfiltered = keep === undefined,
        keys = listToSet(keep);
    if (Ext.isObject(obj)) {
        Ext.Array.each(Object.keys(obj), function(key) {
            if (unfiltered ||  (!unfiltered &amp;&amp; keys[key])) {
                result[key] = obj[key];
            }
        });
    }
    return result;
};

Ext.onReady(function () {
    main();
});
.as-console-wrapper { top: 0; max-height: 100% !important; }
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/extjs/4.2.1/ext-all.js"&gt;&lt;/script&gt;




Without ExtJS Dependency



function main() {
    var Types = {
        INTEGER:   { value: 'int'    },
        CHARACTER: { value: 'char'   },
        FLOAT:     { value: 'float'  },
        DOUBLE :   { value: 'double' },
        STRING:    { value: 'str'    },
        BOOLEAN:   { value: 'bool'   }
    };
    
    var Numbers = filterByKeys(Types, [ 'INTEGER', 'FLOAT', 'DOUBLE' ]);
    
    console.log(Numbers);
};

function listToSet(list) {
    var _set = {};
    if (isArr(list)) {
        for (var i = 0; i &lt; list.length; i++) {
            _set[list[i]] = true;
        }
    }
    return _set;
};

function filterByKeys(obj, keep) {
    var result = {},
        unfiltered = keep === undefined,
        keys = listToSet(keep);
    if (isObj(obj)) {
        for (var key in obj) {
            if (unfiltered ||  (!unfiltered &amp;&amp; keys[key])) {
                result[key] = obj[key];
            }
        };
    }
    return result;
};

var tObj='[object Object]',tArr='[object Array]',str=Object.prototype.toString;
var isArr='isArray'in Array?Array.isArray:function(v){return str.call(v)===tArr};
var isObj=str.call(null)===tObj?function(v){return v!==null&amp;&amp;v!==void 0&amp;&amp;str.call(v)===tObj&amp;&amp;v.ownerDocument===void 0}:function(v){return str.call(v)===tObj
};

$(document).ready(function () {
    main();
});
.as-console-wrapper { top: 0; max-height: 100% !important; }
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;
Use var myKeys = Object.keys(myObject) to get the keys.
Check if a myString exist in the array myKeys using native

var matchingKey = myKeys.indexOf(myString) !== -1


Processing an array? Use this:

var matchingKeys = myKeys.filter(function(key){ return key.indexOf(myString) !== -1 });

Get the value using myObject[matchingKey].

Processing an array? Use this:

var matchingValues = matchingKeys .map(function(key){ return myObject[key] });

No need to check array-boundaries or sizes. It's 100% boundary-safe.