Search code examples
javascriptnode.jsreactjsreact-hooksuse-effect

javascript array logic not rendering properly in react with map component


This is a pretty interesting issue I'm facing. So basically I have two files:

When dtmf is 1, found should return true and the rest of the code should run smoothly. But for some reason, found returns false which ruins the rest of the code. I tested the logic in a separate js compiler with the values in the screenshot below and the logic worked fine.

Not sure why React is behaving this way. It seems as if the variables are not being rendered properly. Maybe need to wait for them to be fully initialized or something?

Let me know. Been working at this for a while and got nowhere. Any help would be very appreciated. Console output


Solution

  • The problem is that you're comparing two different types:

    • Your dmtfs is an array of { value: string; label string } objects. Even though the strings represent numbers, they're still strings.
    • Your ìvr_destsuses actual numbers for thedtmf` field.

    Therefore, you're comparing '1' === 1 which returns false due to being different types.