I have an array have built up with a series of objects in it, but when i am trying to use grep to bring back just one object based on a condition, it is always returning undefined
Array is like
[Object { Id=61, Name=”A Name”, Department=”A Department”, Tag=”A Tag”}, Object {Id=62....
Now the id i am passing in, is not the index, it is the actual Id of the object..ie: 61
var resultOut = $.grep(myArray, function (e) {
return e.Id === id;
});
console.log(resultOut[0]);
Now it has been mentioned that it may be because it is changing the id i am passing in as a string, so the e.Id === id does not match, ut i have no way of checking this
If it's id
being a string, two ways to fix it. Use ==
:
return e.Id == id;
Or convert id
to int:
return e.Id === +id;
// ^
The +
before id
is equivalent to a parseInt()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.