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?
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?
-
recipient
refers to the object, soreturn recipient
will always result in atrue
result, meaning the first iteration throughfind()
will returntrue
and stop. What are you trying to check? Maybe you want something more likereturn recipient.clientId === clientIdVariable
, as an example. – Tyler Roper Commented Oct 9, 2019 at 20:23 - @TylerRoper Sorry, I accidentally posted the question before I was done with it. :/ – TheDeveloperNextDoor 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. YourcheckId
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. – ray Commented Oct 9, 2019 at 20:27
3 Answers
Reset to default 7find
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' })
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 pare any property of it with recipient.
var found = clients.find(function(element) { return element.prop1 === recipient.anyProp; });
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);