Home Ask Login Register

Developers Planet

Your answer is one click away!

Julius Doan February 2016

Removing an array of indexes from an given array

So I am having a bit of difficulty trying to think of the best way of doing this in javascript. I want to remove an array of unsorted indexes from an array that was X number of elements. For example

var index = [ 0, 7, 10, 2, 5, 11] array = [{field0: 0}, {field1: 1}, {field2: 2}, ... {field5: 5}, {field6: 6}...]

So I tried using a nested for loop with splice, but then when I splice, my array loses its indexing and screws up.

The end result should come out to be like array = [{field1: 1}, {field3: 3}, {field4: 4}, .... {field6: 6} ...]

Any help would be greatly appreciated.

Answers


Dave Bush February 2016

I would iterate through array (for/next loop) and at each element see if the number exist in index (index.indexOf(n)). If it does, push the element to a new temporary array.

when you are done, either copy the temp array back to array OR array.length = 0 and push the elements from the temp array back onto the original array.

something like this:

var index = [ 0, 7, 10, 2, 5, 11]
var array = [{field0: 0}, {field1: 1}, {field2: 2},{field3: 3}, {field4: 4}];
var tempArray = [];

for(var arrayIndex = 0;arrayIndex < array.length;arrayIndex++){
    if(index.indexOf(arrayIndex) === -1)
    {
        tempArray.push(array[arrayIndex]);
    }
}
array = tempArray;


Bennett Adams February 2016

If sorting the array of elements to remove is not an issue, you can just do the following:

  var array = [{field0: 0}, {field1: 1}, {field2: 2}, {field3: 3}, {field4: 4}, {field5: 5}, {field6: 6}, {field7: 7}, {field8: 8}, {field9: 9}];
  var indices = [0, 7, 2, 5];

  indices.sort(function(a, b) {
      if ( a > b ) { 
          return 1;
      } else if ( b > a ) { 
          return -1;
      } else {
          return 0;
      }
  });
  for (var i = 0, offset = 0; i < indices.length; i++, offset++) {   
      array.splice(indices[i] - offset, 1);
  }
  console.log(array);

And if for some reason you were unable to sort the array of indices to remove, you could keep track of the offset with the following:

  var array = [{field0: 0}, {field1: 1}, {field2: 2}, {field3: 3}, {field4: 4}, {field5: 5}, {field6: 6}, {field7: 7}, {field8: 8}, {field9: 9}];
  var indices = [0, 7, 2, 5];

  var removedIndices = [];
  function calcOffset(val) {
      var numRemoved = 0;
      for (var j = 0; j < removedIndices.length; j++) {
          if (val > removedIndices[j]) {
              numRemoved++;
          }
      }
      return numRemoved;
  }

  for (var i = 0, offset = 0; i < indices.length; i++, offset++) { 
      var offset = calcOffset(indices[i]);  
      array.splice(indices[i] - offset, 1);
      removedIndices.push(indices[i]);
  }

  console.log(array);


georg February 2016

The solution to your problem is really a one-liner:

array = [00,11,22,33,44,55,66,77];
indexes = [1,7,5,3];

array = array.filter(function(_,i) { return indexes.indexOf(i) < 0 });

document.write('<pre>'+JSON.stringify(array,0,3));


Sirjan Sharma February 2016

You can try deleting them with delete array[index[count]] by looping as long as undefined is not a problem.

Or, you can set loop through all the elements in the array and set any element whose index is not present to false. Then, loop through them again and copy only the ones which are not false to new array.

newArray = new Array();

for (var i = 0; i < array.length; i++) {
  for(var j=0; j<index.length; j++) {
     if (index[j] == i) {
        array[i] = false;
     }
  }
};

for (var i = 0; i < array.length; i++) {
     if (array[i] != false) {
        newArray[i] = array[i];
     }
  }

Post Status

Asked in February 2016
Viewed 2,639 times
Voted 12
Answered 4 times

Search




Leave an answer


Quote of the day: live life