How to pass a value in a function in javascript?

I have an array called collection. Now i want to make a function which looks like this. For every item in "collection" there is an object with id and a status. The id will be provided by clicking on a button which call the function:

            doSomething(id) {
                if (this.collection.id.status == 23) { //doesnt work
                    something happens
                } else {
}               something happens
            }

How do I pass the value of id in the if statement?

I tried to pass the value of id in the if statement but it didnt work.

>Solution :

You will need to use .find() to make your statement works, like this:

doSomething(id) {
  const item = this.collection.find(col => col.id == id);
  if (item .status == 23) {
    ...something happens
  } else {
    ...something happens
}

Leave a Reply