0

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.

8
  • 3
    methinks you just use wrong instrument for filtering. seems you need simple array.filter Commented Dec 2, 2015 at 17:00
  • 1
    Why are you doing array.splice inside the forEach? Do you really need to run splice on the array 3 times? Commented Dec 2, 2015 at 17:02
  • 1
    @RocketHazmat, exactly what my question was going to be. OP you seem to have a misunderstanding of how splice works. Commented Dec 2, 2015 at 17:04
  • 2
    splice modifies indices, forEach starts from 0 and counts up. When it reaches a non-defined index it stops. Because the length changed, the non-defined index is earlier. Therefore forEach is invoked less than initial length times Commented Dec 2, 2015 at 17:19
  • 1
    @JasonMacAnLighiche: Do not edit your solution into the question. The question should remain a question. What you should do is accept the correct answer. Commented Dec 2, 2015 at 18:50

1 Answer 1

2

So you want to remove objects from an array that has archived: true. Here's a couple of ways to do this:

You can use filter which returns a new filtered array:

var arr = dataset.filter(function (el) {
  return el.archived !== true;
});

Or you can use splice to remove elements as you iterate over them:

for (var i = dataset.length - 1; i >= 0; i--) {
  if (dataset[i].archived === true) dataset.splice(i, 1);
}

DEMO

Sign up to request clarification or add additional context in comments.

4 Comments

seems sample with splice wrong - when we remove item we should not increment i variable
Doh. Thanks for the headsup.
@Andy instead of decrementing i for each match or "not incrementing" it, it is actually easier to loop downwards instead as these indices won't be changed var i = dataset.length; while (i-- > 0) {...}
@PaulS., thanks. Sometimes you repeat code enough times it becomes habit and never think there might be a better way of writing it until someone points it out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.