JavaScript错误:无法读取未定义的属性"includes" [英] JavaScript error: Cannot read property 'includes' of undefined
本文介绍了JavaScript错误:无法读取未定义的属性"includes"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想检查 msgArr
数组中是否已经存在 data.objectId
.为此,我正在运行以下代码:
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});
}
该数组如下所示:
var msgArr = [
{isOnline:true,latLont:"123",objectId:"on0V04v0Y9"},
{isOnline:true,latLont:"1",objectId:"FpWBmpo0RY"},
{isOnline:true,latLont:"48343",objectId:"Qt6CRXQuqE"}
]
我收到以下错误:
无法读取未定义的属性包含"
Cannot read property 'includes' of undefined
推荐答案
正如评论所说:javascript数组对象没有属性 objectId
.
查看此数组中的对象,很显然它们具有对象,因此可以使用
As 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);
这篇关于JavaScript错误:无法读取未定义的属性"includes"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文