7

I have an array, clients, which I want to run array.find() on. This array contains objects, and usually looks something like this:

[ { customId: 'user1', clientId: 'TPGMNrnGtpRYtxxIAAAC' },
  { customId: 'user2', clientId: 'G80kFbp9ggAcLiDjAAAE' } ]

This is where I encounter a problem. I am trying to use find() to see if any object (or part of an object) in the array matches a certain variable, recipient, which usually contains a value like user1. the code I am using to do this is:

function checkID(recipient) {
       return recipient;
        }
    var found = clients.find(checkID);

This always returns the first object in the array. Am I using find() wrong, or is there a better way to do this?

3
  • recipient refers to the object, so return recipient will always result in a true result, meaning the first iteration through find() will return true and stop. What are you trying to check? Maybe you want something more like return recipient.clientId === clientIdVariable, as an example. Commented Oct 9, 2019 at 20:23
  • @TylerRoper Sorry, I accidentally posted the question before I was done with it. :/ Commented Oct 9, 2019 at 20:24
  • find takes a function that receives an element from the array and returns a boolean indicating whether the element matches a condition. Your checkId function always returns the element it receives, which gets cast to a boolean, which will be true as long as the element itself isn’t falsy (false, null, 0, undefined), so it’s no surprise that this always returns the first element. Commented Oct 9, 2019 at 20:27

3 Answers 3

7

find takes a predicate (a function that returns true if item is a match and false if item is not a match).

const arr = [ { customId: 'user1', clientId: 'TPGMNrnGtpRYtxxIAAAC' },
              { customId: 'user2', clientId: 'G80kFbp9ggAcLiDjAAAE' } ]

const result = arr.find(item => item.customId === 'user1')
                             // ^^^^^^^^^^^^^^^^^^^^^^^^^ 
                             // This should evaluate to true for a match and to false for non-match

The reason you're getting the first item of your array all the time, is because your checkId function is returning something which evaluates to true. So, the first item is evaluated and produces a truthy result, and therefore it gets picked as the first match.


If unfamiliar with the lambda syntax () => {}, then that line is similar to:

const result = arr.find(function (item) { return item.customId === 'user1' })
Sign up to request clarification or add additional context in comments.

2 Comments

The function must return true if match, and false if not match
@TylerRoper Right, I misread OPs code initially. Thanks to your tip, I edited the response. Thanks :)
1

You are using find wrong. If recipient contains information about the target value you should name the first param of checkID with a different name. And compare any property of it with recipient.

var found = clients.find(function(element) { return element.prop1 === recipient.anyProp;  });

Comments

0

To check the objects in the array for the presence of a certain customId, put the value you're searching for in an object, and pass that object to find():

let clients = [{
    customId: "user1",
    clientId: "TPGMNrnGtpRYtxxIAAAC"
  },
  {
    customId: "user2",
    clientId: "G80kFbp9ggAcLiDjAAAE"
  }
];

function checkID(el){
  return el.customId === this.param;
}

let found = clients.find(checkID, {param: "user1"});
console.info(found);

Comments

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.