I came across a course, where the instructor used the following function arguments
const filteredBody = filteredObj(req.body, "name", "email");
Then he defined the function as the follows:
const filteredObj = (obj, ...allowedFields) => {
const newObj = {};
Object.keys(obj).forEach((el) => {
if (allowedFields.includes(el)) {
newObj[el] = obj[el];
}
});
return newObj;
};
I am new to Javascript and I am having trouble understanding how he converted the arguments "name","email"
into an array of allowedFields
using ...allowedFields
.