[Vuejs]-Javascript: Remove Item from Array

1πŸ‘

βœ…

I think you need to remove the property directly from the array, you don’t get anything removing the reference out of the parameter.

removeAllArchived: function() {
    this.tasks.forEach(function(task, index) {
        if(task.archived == true) {
            console.log(task.task);
            delete(this.tasks[index]);
        }
    });
}

Might be better if you filter out the array:

removeAllArchived: function() {
    this.tasks = this.tasks.filter(function(task){
         return task.archived !== true;
    });
}

Edit: It cleaned out non archived. Now it removes archived.

πŸ‘€MinusFour

1πŸ‘

You can use slice, or I like and use this snip from here: http://ejohn.org/blog/javascript-array-remove/

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

Leave a comment