Consider the following:
this.setState({
validations: [...this.state.validations, ...validations]
}, () => {
// ... Do something
});
Where I am doing: [...this.state.validations, ...validations]
validations is an an array of objects.
So for example:
// validation
[{message: 'hello', isError: true, fieldId: 87}, ....]
Essentially every time we have a validation error we set an array of objects to an array as you see above.
The issue is that sometimes duplicate objects get added. By duplicate I mean on the message section, so this.state.validations
could look like:
[
{message: 'hello', isError: true, fieldId: 87},
{message: 'hello', isError: true, fieldId: 87},
{message: 'hello', isError: true, fieldId: 87},
...
]
What I would like to do is filter this.state.validations
based on the message and fieldId in validations
and say:
// If this message doesn't exist for this field id, set it.
Is there a simple non messy way of doing this where I could use filter or something, to loop over the state validations and the validations comparing both fieldId and message and if it doesn't exist: add it?