Created
February 9, 2017 11:40
-
-
Save pawiromitchel/50a1c23178751d8bd532c151e9ec3f8b to your computer and use it in GitHub Desktop.
JS - search-in-object-values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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