JavaScript React-native array check if value exist react native array map,react native array length,react native

How do check if ticketId already inserted into array?

This is the code:

state = { myState: [], status: 0, limit: 5 };

  selected(ticketId) {
  if (this.state.status < this.state.limit) { 
    this.state.myState.push({ id: ticketId }); //what ever i chose get inserted
    this.state.status++;
    this.setState({ status: this.state.status }); 
    //console.log('here', this.state.status);
  } else {
    console.log('3'); //check if condition meet
  }
 }

How do I create a condition checking myState array if the ticketId already insert and trigger an alert?

Answer:1

As you are storing as an object {id:value}, better use "every" instead.

state = { myState: [], status: 0, limit: 5 };

selected(ticketId) {
   if (this.state.status < this.state.limit ) { 
      if(this.state.myState.every((item) => item.id !== ticketId)){
        this.state.myState.push({ id: ticketId }); //what ever i chose get inserted
        this.state.status++;
        this.setState({ status: this.state.status }); 
      //console.log('here', this.state.status);
     }
   } else {
   console.log('3'); //check if condition meet
   } 
}
Answer:2

I'm using angular ui , I've created a directive that its controller listen to the websockets server and retrieve the data, actually it works fine but when leaving the page that loads the directive the ...

I'm using angular ui , I've created a directive that its controller listen to the websockets server and retrieve the data, actually it works fine but when leaving the page that loads the directive the ...

I am trying to upload pictures without the page refreshing. I used event.preventDefault() in order to stop the page from refreshing. However, the PHP code that is inside of my jQuery code is not being ...

I am trying to upload pictures without the page refreshing. I used event.preventDefault() in order to stop the page from refreshing. However, the PHP code that is inside of my jQuery code is not being ...

I am using eval() to run a script from a string. Below is the code: eval('console.log("hello")'); I will get hello from the console output. I wonder whether I can save the hello into an variable in ...

I am using eval() to run a script from a string. Below is the code: eval('console.log("hello")'); I will get hello from the console output. I wonder whether I can save the hello into an variable in ...

  1. console output from windows service
  2. console output from wpf application
  3. console output from python
  4. console output from java
  5. console output from gradle
  6. console output in c#
  7. console output in javascript
  8. console output in visual studio
  9. console output in jenkins
  10. console output in php
  11. console output in intellij
  12. console output in android studio
  13. console output in visual studio 2017
  14. console output in powershell
  15. console output in typescript
  16. console output in visual studio code
  17. console output in js
  18. console output in jsp
  19. console output in r
  20. console output in html

There is two constructor functions: Animal and Rabbit (classes emulation). How can I set inheritance for this classes using prototypes properly? I seen 2 versions: Rabbit.prototype.__proto__ = Animal....

There is two constructor functions: Animal and Rabbit (classes emulation). How can I set inheritance for this classes using prototypes properly? I seen 2 versions: Rabbit.prototype.__proto__ = Animal....