I'm doing an assignment using javascript language for educational purposes, but i'm a beginner. I'm having some problems trying to use reduce function.
What i'm trying to do is to use reduce to filter the following.
"Receive a string array in propNames and an object array in objs. Return a new object array with objects produced by applying the filterProperties function with propNames to each object in objs."
I already code filterProperties and its working fine to match propNames with only one object. I leave the code down below:
function filterProperties(propNames, obj) {
let output = {}
let objKeys = Object.keys(obj);
const matchingElems = propNames.sort().filter(elem => objKeys.includes(elem))
matchingElems.forEach(elem => output[elem] = obj[elem])
return output
}
So just to sum up, i would like to use reduce to iterate and output an array with the properties matching propNames and objs.
My code is not working, i'm a complete beginner and I didn't understand well how to use reduce function. I leave my code below, so that anyone can help me solving this issue.
function filterPropertiesN(propNames,objs) {
objs.reduce(filterProperties(propNames, objs))
}
many thanks in advance!