Skip to content
Advertisement

How can I create conditions on multiple array objects to handle errors in React?

I have an array object called containtemps. It has a property value called modDate.

I extract the boolean value of the temperror variable by comparing the time based on the modDate property value among several objects in containtemps.

So, by putting temperror variables 1, 2, and 3 in the error variable, if any one of them is true, the NotError component is rendered, and when all of them are false, the Error component is rendered.

But looking at my code, this seems inefficient because there are too many duplicates.

As you can see, temperror0 and temperror1 and temperror2, The code that creates the variable is duplicated. How can I get rid of these duplicate values ​​in one piece of code?

this is my code

JavaScript

Advertisement

Answer

You can use some function inside your array. It will return true if it finds at least one result matching your expression. Here you need the opposite expression >

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement