How does this output make any sense? Maybe I am thinking about it wrong but it seems fairly descriptive in what it's supposed to be dong
var dataset = [1, 2, 3];
dataset.forEach(function(element, index, array) {
// (index, how many to remove)
array.splice(0, 0);
});
returns [1,2,3] as expected
dataset.forEach(function(element, index, array) {
array.splice(0, 3);
});
returns [] as expected
dataset.forEach(function(element, index, array) {
array.splice(0, 1);
});
returns [3]
dataset.forEach(function(element, index, array) {
array.splice(0, 2);
});
returns [3]
This has got me questioning my understanding of everything lol. I had a more complex requirement where an array of objects may have property "element.archived"
var dataset = [Object, Object, Object]
dataset.forEach(function(element, index, array) {
if (element.archived) array.splice(index, 1);
});
I am trying to iterate over the array, and remove all objects that have this property value of true.
array.spliceinside theforEach? Do you really need to runspliceon the array 3 times?spliceworks.splicemodifies indices,forEachstarts from0and counts up. When it reaches a non-defined index it stops. Because the length changed, the non-defined index is earlier. ThereforeforEachis invoked less than initial length times