Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
0
rated 0 times [  7] [ 7]  / answers: 1 / hits: 86459  / 8 Years ago, fri, january 6, 2017, 12:00:00

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



More From » typescript

 Answers
6

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);

[#59435] Wednesday, January 4, 2017, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
josefn

Total Points: 251
Total Questions: 93
Total Answers: 84

Location: Senegal
Member since Fri, Aug 21, 2020
4 Years ago
;