1

I am trying to get the value of the client using the client's name and for some reason I get back the full client object:

function createBank(clients) {
    return {
        clients: clients,
        safeBoxValue: function() {
            return  this.clients.reduce(function(sum, client) {
                return sum + client.value;
            }, 0);
        },
        getclientValue: function(clientName) {
            return this.clients.find(function(client) {
                if (client.name === clientName) {
                    return client.value;
                }
            });
        }
    }
}
var clients = [
    {name: "John", value: 349},
    {name: "Jane", value: 9241},
    {name: "Jill", value: 12734},
]
var bank = createBank(clients);
bank.safeBoxValue(); // 22324
bank.getclientValue('Jill'); // {"name":"Jill","value":12734}

Anybody knows why? Thanks!

drjorgepolanco
  • 7,479
  • 5
  • 46
  • 47
  • 1
    Are you sure you know how [`find`](https://developer.mozilla.org/en-US/docs/We…) works ? because you seem to be working under the assumption that it's also a `map`, which it isn't. Its argument should be a predicate function that returns a truthy or falsy value. – Touffy Oct 29 '17 at 06:42
  • 1
    Thanks @Touffy! That being said I could just do: return (client.name === clientName) and would get the same result. Would just access it as @Sajeetharan suggested. – drjorgepolanco Oct 29 '17 at 06:52
  • 1
    Possible duplicate of [Find object by id in an array of JavaScript objects](https://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects) – hawk Oct 29 '17 at 06:55
  • If you are calling the method `getclientValue()` it should return the value. It shouldn't expect the caller to the call `.value()` on the returned item. – Mark Oct 29 '17 at 06:56

1 Answers1

3

array.find() works by passing in a function that returns a Boolean value to determine whether the object is the one you're looking for. Your function is working despite the code because you are returning a value that is 'truthy' when you return client.value.

The function would work exactly the same if you had just done this:

getclientValue: function(clientName) {
    return this.clients.find(function(client) {
        return client.name === clientName
    });
}

It will go through the array until you return true and then pass you the element, in this case the whole object, you just found. To only get the value, you will need to return it separately:

getclientValue: function(clientName) {
    var found =  this.clients.find(function(client) {
        return client.name === clientName
    });
    return found && found.value
}

Just remember find() only returns the first value found.

Mark
  • 90,562
  • 7
  • 108
  • 148
  • Have you read the doc? Find return a value in the array if an element passes the test; otherwise, undefined. – hawk Oct 29 '17 at 06:56
  • your code will throw exception if element was not found, how it can be correct answer? – hawk Oct 29 '17 at 06:58