Hello there,
Query: Can you provide guidance on constructing a function that does the following:
- Accepts an object as the first argument;
- Takes a schema model as the second argument;
- The second argument is an array of objects with different models than the first argument;
Objective: The desired outcome is to return an array of objects with the following modifications:
- All properties that do not exist in the first argument (object model) should be removed from each element;
- Non-existent properties in each element should be created with a NULL value;
- Existing properties in each element should remain unchanged;
Example - Function call:
standardizeWith({id: 1, name:'abcd'}, [{name:'Carlos', age:30}, {a:'x', b:'y', c:'z'}])
// **output:**
// 0:{name: "Carlos", id: null}
// 1:{name: null, id: null}
const standardizeWith = (object,array) => array.reduce(
(accumulator, { id, name}, index) => (
{
...accumulator,
[index]: {id, name}
}),
{}
);
console.log(standardizeWith({id: 1, name:'abcd'}, [{name:'felipe', age:27}, {a:'x', b:'y', c:'z'}]));