I’m trying to make a logger for IDs and names, but I can’t seem to detect if the script is trying to add the two same IDs.
I’ve tried id.includes(user._id)
, but it always passed the if
test. Here’s the code:
JavaScript
x
27
27
1
let id = [];
2
MPP.client.on('participant added', user => {
3
if (id.includes(user._id))
4
return console.log(
5
`%c - joined, seen ${user._id}, n: ${user.name}`,
6
'color: red;'
7
);
8
id.push({ id: user._id, n: user.name, action: 'joined' });
9
console.log(
10
`%c + joined, added ${user._id} to list, n: ${user.name}`,
11
'color: lime;'
12
);
13
});
14
MPP.client.on('participant removed', user => {
15
if (id.includes(user._id))
16
return console.log(
17
`%c - left, seen ${user._id}, n: ${user.name}`,
18
'color: red;'
19
);
20
id.push({ id: user._id, n: user.name, action: 'left' });
21
console.log(
22
`%c + left, added ${user._id} to list, n: ${user.name}`,
23
'color: lime;'
24
);
25
});
26
27
Advertisement
Answer
You can search your id
array with find
this will give you the current object which you can match your user._id
against.
JavaScript
1
3
1
if (id.find(participant => participant.id === user._id))
2
return
3