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 ?

Aod Ren
  • 681
  • 1
  • 8
  • 17
  • 1
    You return only from your closure. I.e forEach. Your external function only returns false and thats what you are seeing – Hanky Panky Feb 20 '16 at 18:09
  • 1
    You are returning true only inside the forEach() function. The function you are wondering about always returns false – baao Feb 20 '16 at 18:11

4 Answers4

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;
    });
}
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
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);
}
madox2
  • 49,493
  • 17
  • 99
  • 99
1

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

David Deutsch
  • 17,443
  • 4
  • 47
  • 54
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).

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214