简体   繁体   English

尝试访问数组中的对象时,grep返回未定义

[英]grep returning undefined when trying to access object in array

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 我有一个数组,其中包含一系列对象,但是当我尝试使用grep根据条件带回一个对象时,它总是返回未定义的

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 现在我要传递的ID不是索引,而是对象的实际ID。即: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 现在已经提到,可能是因为它正在更改我以字符串形式传递的id,所以e.Id === id不匹配,但我无法检查此

If it's id being a string, two ways to fix it. 如果id是字符串,则有两种方法可以修复它。 Use == : 使用==

return e.Id == id;

Or convert id to int: 或将id转换为int:

return e.Id === +id;
//              ^

The + before id is equivalent to a parseInt() . id之前的+等效于parseInt()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试从数组访问信息时,React JS 返回未定义 - React JS is returning undefined when trying to access information from array 尝试访问嵌套在数组中的 object 的属性时未定义的类型 - Typeof undefined when trying to access property of object nested in array 尝试访问数组对象中的属性时未定义 - Getting undefined when trying to access properties in array object 尝试访问ReactJS中嵌套在对象中的数组时获取未定义 - Get Undefined when trying to access array nested in Object in ReactJS 尝试使用 find() function 返回未定义的数组的 object - Trying to return an object of an array returning undefined using the find() function 尝试在事件处理程序中访问时,画布以未定义的方式返回 - Canvas Returning As Undefined When Trying To Access In Event Handeler 数组中的字符串仅在尝试获取单个值时才返回 undefined? - String in array returning undefined only when trying to get single value? 尝试访问 JS 中的数组对象给我未定义 - Trying to access array object in JS gives me undefined 尝试使用引用访问兄弟组件的TextInput时“未定义不是对象” - “undefined is not an object” when trying to access TextInput of the sibling component using references 尝试访问 javascript 中的 object 属性时出现“未定义” - Getting "undefined" when trying to access an object property in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM