Hi,
Question: How can I build a function that:
- Recive a object by argument;
- It's a schema model for the second argument;
- The second argument is a array of objects that has not the same model of the first argument;
Objective: The return need be a array of objects with this modifications:
- Need be removed each of each element the properties that does't exist on the first argument (the object model);
- For the properties that doesn't exist on element need be created with NULL value;
- Finally, the other properties of each element need persist with the same value;
Example - call function:
padronizarCom({id: 1, nome:'abcd'}, [{nome:'Carlos', idade:30}, {a:'x', b:'y', c:'z'}])
// **output:**
// 0:{nome: "Carlos", id: null}
// 1:{nome: null, id: null}
const padronizarCom = (object,array) => array.reduce(
(accum, { id, nome}, i) => (
{
...accum,
[i]: {id, nome}
}),
{}
);
console.log(padronizarCom({id: 1, nome:'abcd'}, [{nome:'felipe', idade:27}, {a:'x', b:'y', c:'z'}]));