In addition to the ideas in the other answers, you'd better use Array.prototype.some rather than forEach. This will allow you to stop when you find the first match:
function getByKey(key) { var found = null; data.some(function (val) { if (val.Key === key) { found = val; return true;
You can also use a filter that can return an array containing only those objects where the key is located:
function filter_array_by_key(key){ return data.filter(function(v){ return v.Key===key; }; }
To get the first matching object, you can use filter_array_by_key(key)[0] , which will give undefined if there was no match.
user663031
source share