admin管理员组文章数量:1187338
I want to check if a data.objectId
already exists in the array msgArr
. For that I am running the code below:
var exists = msgArr.objectId.includes(data.objectId);
if(exists === false){
msgArr.push({"objectId":data.objectId,"latLont":data.latLont,"isOnline":data.isOnline});
}
The array looks like the following:
var msgArr = [
{isOnline:true,latLont:"123",objectId:"on0V04v0Y9"},
{isOnline:true,latLont:"1",objectId:"FpWBmpo0RY"},
{isOnline:true,latLont:"48343",objectId:"Qt6CRXQuqE"}
]
I am getting the error below:
Cannot read property 'includes' of undefined
I want to check if a data.objectId
already exists in the array msgArr
. For that I am running the code below:
var exists = msgArr.objectId.includes(data.objectId);
if(exists === false){
msgArr.push({"objectId":data.objectId,"latLont":data.latLont,"isOnline":data.isOnline});
}
The array looks like the following:
var msgArr = [
{isOnline:true,latLont:"123",objectId:"on0V04v0Y9"},
{isOnline:true,latLont:"1",objectId:"FpWBmpo0RY"},
{isOnline:true,latLont:"48343",objectId:"Qt6CRXQuqE"}
]
I am getting the error below:
Share Improve this question edited Aug 8, 2019 at 18:09 isherwood 61k16 gold badges120 silver badges168 bronze badges asked Jan 6, 2017 at 18:02 Folky.HFolky.H 1,2044 gold badges16 silver badges40 bronze badges 6 | Show 1 more commentCannot read property 'includes' of undefined
3 Answers
Reset to default 19As the comments say: the javascript array object has no property objectId
.
Looking at the objects in this array it's clear that they have it, so to check if a certain element exists you can do so using Array.prototype.some method:
var exists = msgArr.some(o => o.objectId === data.objectId);
It's telling you that you're trying to access a property on an undefined object. The msgArr
object doesn't have a property objectID
at all, which means it's undefined. Since that doesn't exist, there's no way for it to have an includes
property available of any type.
What you need is to access an object in the array, not the array itself. Something like msgArr[0].objectID
would refer to an instantiated object. You could even use the array functions to check if something exists based on its objectID with a filter function.
First of all Dave Newton is right. An array doesn't have an objectId!
Maybe your "array" isn't a real array. Maybe it's an object which contains an array... I don't know... but in that case, you would have to code something like this:
var exist = msArr["objectId"] !== undefined
than "exist" which is a boolean contains the info if "msArr" has an property/field called "objectId"
本文标签: typescriptJavaScript error Cannot read property 39includes39 of undefinedStack Overflow
版权声明:本文标题:typescript - JavaScript error: Cannot read property 'includes' of undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738284367a2072880.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
msgArr
is an array. It doesn't have anobjectId
. – Dave Newton Commented Jan 6, 2017 at 18:04msgArr
is an array. It doesn't have anobjectId
. – user1106925 Commented Jan 6, 2017 at 18:06