I have an array called arr with objects nested inside of it as seen in the example below:
[{"type" : "space", "content" : "value", /*etc.*/},
{"type" : "space", "content" : "value", /*etc.*/},
{"type" : "other value", "content" : "value", /*etc.*/},
/*Other nested objects*/
];
So i'm trying to loop through the array to check if the object after the one that the loop is currently "focused" on contains the 'type' property then check if it's type property is set to "space", if it is it will remove it from the array
This is the bit of code that is turning up the type error:
for (var i = 0; i < arr.length; i++){
if (arr[i].type && arr[i + 1].type){
if (arr[a].type == "space" && arr[a + 1].type == "space"){
arr.pop(arr[a]);
}
}
}
Am I doing something wrong as it does not seem to be happy with the arr[i + 1] on the second line
Please ask if you would like me to expand on anything that I have not made clear.
Many thanks.
arr.pop()alters the array and its size while the loop is still iterating over it. This may lead to unexpected results, like indexi+1not being defined.