简体   繁体   English

如何比较 JavaScript (React-Native) 中的两个动态变量?

[英]How to compare two dynamic variables in JavaScript (React-Native)?

I am trying to compare these two variables: res_id;我试图比较这两个变量: res_id; and rest_id;rest_id;

likes this:喜欢这个:

if(res_id == rest_id){
console.log("THESE VALUES ARE THE SAME", rest_id);
}

whenever any of their values are the same I want to print something but it doesn't work using the above statement.每当它们的任何值相同时,我都想打印一些东西,但使用上述语句不起作用。

Remember they are dynamic.请记住,它们是动态的。

console.log() :控制台日志():

    [14:21:08] I | ReactNativeJS ▶︎ 'PRINT THE REST-ID:', '1'
                             └ 'PRINT THE RES-ID:', undefined

[14:21:08] I | ReactNativeJS ▶︎ 'PRINT THE REST-ID:', 'Aura1'
                             └ 'PRINT THE RES-ID:', undefined

[14:21:08] I | ReactNativeJS ▶︎ 'PRINT THE REST-ID:', 'jeta1'
                             └ 'PRINT THE RES-ID:', undefined

[14:21:08] I | ReactNativeJS ▶︎ 'PRINT THE REST-ID:', undefined
                             └ 'PRINT THE RES-ID:', '1'

[14:21:08] I | ReactNativeJS ▶︎ 'PRINT THE REST-ID:', undefined
                             └ 'PRINT THE RES-ID:', '10'

You've provided no view of your overall function, but i'm assuming it's a react component in which case you can use useEffect to perform an action when any of those values change.您没有提供整体功能的视图,但我假设它是一个反应组件,在这种情况下,当这些值中的任何一个发生变化时,您可以使用 useEffect 来执行操作。

useEffect((), {
    if (res_id === rest_id) {
     console.log('they are equal')
    }
    else {
      console.log('they are not equal')
    }
  }, [res_id, rest_id])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM