Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading