I would like to ask how to remove an element inside an array if the value of object matches with the condition.
For example:
[
{
"id": 1,
"sender": "a",
"receiver": "b"
},
{
"id": 2,
"sender": "b",
"receiver": "c"
}
{
"id": 3,
"sender": "a",
"receiver": "c"
}
]
I want to remove the element if sender and/or receiver has a value of "a", which in this case, ids 1 and 3. How do I do that? I have read about splice and I tried to for loop but was not able to accomplish what I wanted.
This was what I tried:
for (let index = 0; index < arr.length; index++) {
if (arr[index].sender == "a") {
arr.splice(index.sender, 1);
}
if(arr[index].receiver == "a") {
arr.splice(index.receiver, 1);
}
}
Still, object in array was not removed.