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!

4
  • 1
    Are you sure you know how find 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. Commented Oct 29, 2017 at 6: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. Commented Oct 29, 2017 at 6:52
  • 1
    Possible duplicate of Find object by id in an array of JavaScript objects Commented Oct 29, 2017 at 6: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. Commented Oct 29, 2017 at 6:56

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Have you read the doc? Find return a value in the array if an element passes the test; otherwise, undefined.
your code will throw exception if element was not found, how it can be correct answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.