How to remove elements from can.List in CanJS

309 Views Asked by At

I'm new to CanJS. I was wondering how to remove the elements in can.List not using pop().

var todos1=new can.List([
              {name: "Apple",selected: false},
              {name: "Ball",selected: true},
              {name: "Cat",selected: false}                
            ]);

How to remove an element in the list with selected=true? Or is there any way to delete according to index. Documentation tells pop() is the only way and also splice.

1

There are 1 best solutions below

0
On BEST ANSWER

You can definitely use splice or filter the list by the selected attribute and then replace it:

var todos1=new can.List([
  {name: "Apple",selected: false},
  {name: "Ball",selected: true},
  {name: "Cat",selected: false}                
]);

todos1.replace(todos1.filter(function(todo) {
  return todo.attr('selected') === true;
}));

Now todos1 will only contain the selected elements.