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

Javascript find function

I have an array and trying to use find method and it doesnt give be expected result but i get it via foreach. Sorry I cann’t provide array, because i dont have it . I tried for a long time but didn’t find a mistake. Hope its enough.

When i am using that foreach it works

products.product.forEach(function(p) {
  p.composition.id === id);
} 

But it doesnt work here

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

let currentProduct = products.product.find(pr=> {pr.composition.id===id});

>Solution :

The reason it’s not working is because you’re not returning the boolean of the condition from the .find. Try this:

let currentProduct = products.product.find((pr) => {
    return pr.composition.id === id;
});

Or even just without the curly braces:

let currentProduct = products.product.find((pr) => pr.composition.id === id);
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