I learn Javascript and read and understand how the includes
works to test if a value exists in an array.
My problem comes when I have an Array of items that look like this this:
state = { focused: null, files: [] };
:
The image show this here:
When I add a new newFile
and it's the same it should not add it ok but the evaluation,
if (newFile && !files.includes(newFile)) {..
always say false so how to test for this do I have to test on individual values inside newFiles
?
Array.includes
will not do a deep comparison between object, only a shallow one comparing references. This way even objects that have same values they would fail on test because they have different references.
In your case you could compare id
, which should be unique for each object. You can use Array.every
that checks if all values pass in the test. This way you want to check that every file id doesn't match newFile id:
if (newFile && files.every(({id}) => newFile.id !== id))
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments