I am relatively new to Lodash and I'm trying to do something that I'm sure is really easy.
I'm wondering which existing Lodash method to use to take an array of keys and an object, then return a new object containing only the properties (from the original object) that match the keys that were passed in.
let _ = require("lodash");
// DOES THE FUNCTION BELOW ALREADY EXIST?
let only = function (wanted, srcList) {
return _.reduce(wanted, function(out,key){
if(_.has(srcList, key)){
out[key] = srcList[key]
}
return out;
},{})
}
let src = {
w: "This is doble vuu",
x: "This is X",
y: "This is why?",
z: "This is zeee!"
};
var want = ['x','z'];
let newObject = only(want,src);
console.log(newObject)
The code I have works, but I don't want to re-invent the proverbial wheel, no matter how simple it is.