How can I check if javascript array already contains a specific array

Kid

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:

enter image description 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?

buzatto

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Javascript: How can i check if a array contains certain values

From Dev

How can I use javascript to check if a sharepoint list contains a specific word, when he is already have information

From Dev

How can I check if one two-dimensional NumPy array contains a specific pattern of values inside it?

From Dev

How can I check whether the numpy array exists already or not?

From Dev

How can I check If a nested array contains a value?

From Dev

How can I check if sentences in array contains exact words in Swift?

From Dev

How can I check if the text of a button contains an element of an array?

From Dev

How to check if an array contains a specific number of values?

From Dev

Why can't I check to see if my array of arrays contains a specific array?

From Dev

How can I check if a string contains characters stored in an array but return false if it contains any characters not in the array

From Dev

How can one check if an array contains a substring?

From Dev

How to check an array if it contains a string in Javascript?

From Dev

How to check if childnode of Javascript object contains array

From Dev

How to check if array contains instance of an object in Javascript?

From Dev

Check if array contains specific value

From Dev

PHP : How can I check Array in array?

From Dev

How do I check if an array contains anything else than a specific two character value in bash?

From Dev

How can I check index in object array exist or no in javascript?

From Dev

How can I skip a specific Index in an array in a for loop javascript

From Dev

How can I parse specific parts of my AJAX array in Javascript?

From Dev

Javascript check if array contains value

From Dev

How can I check if an ArrayList<String> contains any elements from an array of Strings?

From Dev

Pandas: How can I check if a pandas dataframe contains a specific value?

From Dev

Pandas: How can I check if a pandas dataframe contains a specific value?

From Dev

Check if array contains an object with the value of a specific property

From Dev

Check if array contains an object with the value of a specific property

From Dev

How can I check whether a specific integer exists in an array column I'm selecting?

From Dev

JavaScript: How to check to see if string contains value in array?

From Dev

How can I use a method to check if the new element that I want to add to my array hasn't already been added?

Related Related

  1. 1

    Javascript: How can i check if a array contains certain values

  2. 2

    How can I use javascript to check if a sharepoint list contains a specific word, when he is already have information

  3. 3

    How can I check if one two-dimensional NumPy array contains a specific pattern of values inside it?

  4. 4

    How can I check whether the numpy array exists already or not?

  5. 5

    How can I check If a nested array contains a value?

  6. 6

    How can I check if sentences in array contains exact words in Swift?

  7. 7

    How can I check if the text of a button contains an element of an array?

  8. 8

    How to check if an array contains a specific number of values?

  9. 9

    Why can't I check to see if my array of arrays contains a specific array?

  10. 10

    How can I check if a string contains characters stored in an array but return false if it contains any characters not in the array

  11. 11

    How can one check if an array contains a substring?

  12. 12

    How to check an array if it contains a string in Javascript?

  13. 13

    How to check if childnode of Javascript object contains array

  14. 14

    How to check if array contains instance of an object in Javascript?

  15. 15

    Check if array contains specific value

  16. 16

    PHP : How can I check Array in array?

  17. 17

    How do I check if an array contains anything else than a specific two character value in bash?

  18. 18

    How can I check index in object array exist or no in javascript?

  19. 19

    How can I skip a specific Index in an array in a for loop javascript

  20. 20

    How can I parse specific parts of my AJAX array in Javascript?

  21. 21

    Javascript check if array contains value

  22. 22

    How can I check if an ArrayList<String> contains any elements from an array of Strings?

  23. 23

    Pandas: How can I check if a pandas dataframe contains a specific value?

  24. 24

    Pandas: How can I check if a pandas dataframe contains a specific value?

  25. 25

    Check if array contains an object with the value of a specific property

  26. 26

    Check if array contains an object with the value of a specific property

  27. 27

    How can I check whether a specific integer exists in an array column I'm selecting?

  28. 28

    JavaScript: How to check to see if string contains value in array?

  29. 29

    How can I use a method to check if the new element that I want to add to my array hasn't already been added?

HotTag

Archive