Skip to content

Instantly share code, notes, and snippets.

@pawiromitchel
Created February 9, 2017 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawiromitchel/50a1c23178751d8bd532c151e9ec3f8b to your computer and use it in GitHub Desktop.
Save pawiromitchel/50a1c23178751d8bd532c151e9ec3f8b to your computer and use it in GitHub Desktop.
JS - search-in-object-values
var a = [{
name: 'xyz',
grade: 'x'
}, {
name: 'yaya',
grade: 'x'
}, {
name: 'x',
frade: 'd'
}, {
name: 'a',
grade: 'b'
}];
function filterIt(arr, searchKey) {
return arr.filter(function(obj) {
return Object.keys(obj).some(function(key) {
return obj[key].includes(searchKey);
})
});
}
console.log("find 'x'", filterIt(a,"x"));
console.log("find 'a'", filterIt(a,"a"));
console.log("find 'z'", filterIt(a,"z"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment