1

I have a javascript function :

function isInSupplier(idsupplier) { suppliers.forEach(function(object) { if (object._id == idsupplier) { console.log("TRUE"); return true; }; }); return false; 

I have a list of products, each have a suppliers. I want to make a list of unique suppliers, therefore is the supplier is already in my suppliers list i will not add a new one.

Here is my function to do so :

 console.log(isInSupplier(('<s:property value="supplier.id" />'))); if (!isInSupplier(('<s:property value="supplier.id" />'))) { suppliers.push(new supplier( ('<s:property value="supplier.id" />'), ('<s:property value="supplier.supplier_name" />'), ('<s:property value="supplier.type" />'), ('<s:property value="supplier.phone" />') )); } 

And there is something i don't understand : even tho the console logs "TRUE" properly, the function doesn't return true. In my second block of code i have another console log; that always logs false.

What is it i'm missing ?

2
  • 1
    You return only from your closure. I.e forEach. Your external function only returns false and thats what you are seeing Commented Feb 20, 2016 at 18:09
  • 1
    You are returning true only inside the forEach() function. The function you are wondering about always returns false Commented Feb 20, 2016 at 18:11

4 Answers 4

5

Please change it to Array.prototype.some()

The some() method tests whether some element in the array passes the test implemented by the provided function.

function isInSupplier(idsupplier) { return suppliers.some(function(object) { if (object._id == idsupplier) { console.log("TRUE"); return true; }; }); } 

or without console output

function isInSupplier(idsupplier) { return suppliers.some(function(object) { return object._id == idsupplier; }); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Ah yes some was what I was looking for, but I can never remember the JS higher-order function names.
2

forEach method takes function as a parameter. If you return inside it you wont return from outer function. You can use Array.prototype.some to make it work:

function isInSupplier(idsupplier) { return suppliers.some(function (object) { return object._id == idsupplier; }); } 

If your environment supports ES6 you can utilize it with arrow function:

function isInSupplier(idsupplier) { return suppliers.some(o => o._id == idsupplier); } 

Comments

1

You are returning true from the function you pass to forEach, not from isInSupplier.

Comments

1

Your return true returns the function passed to forEach, not isInSupplier. Instead you can use findIndex:

function isInSupplier(idsupplier) { var index = suppliers.findIndex(function(object) { return object._id == idsupplier; }); return index != -1 } 

Or really probably better to use some as in Nina’s answer (I couldn’t remember the name so resorted to findIndex).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.