observableArray remove issue

38 views
Skip to first unread message

Lee Blazek

unread,
May 29, 2013, 8:48:35 PM5/29/13
to knock...@googlegroups.com
I am working with observable arrays. I have push() working fine, but I can't get remove to work..  from a jquery function.

Here's what I have for push(working):

viewModel.adj.push({word: item, type: itemType});

And I would think that this would work to, but doesnt:

viewModel.adj.remove({word: item, type: itemType});

Can any spot some thing wrong in that line above?

Ryan Rahlf

unread,
May 29, 2013, 9:44:12 PM5/29/13
to knock...@googlegroups.com
Hi Lee,

Try this instead:

var myItem = {word: item, type: itemType};
viewModel.adj.push(myItem);
viewModel.adj.remove(myItem);

When calling remove, you want to pass in the exact object you'd like to remove, not an identical object.  If you don't have the object you can either search for it, or you can use a function to identify the object you'd like to remove, like this:

//the function will be provided each item in the array for comparison.  
//return true if you'd like the item removed.
viewModel.adj.remove(function(i){
    return i.word == item && i.type == itemType;
});

-Ryan Rahlf

rpn

unread,
May 29, 2013, 9:46:41 PM5/29/13
to knock...@googlegroups.com
For ".remove" you either need to pass the item that you want to remove (a reference to that item) or pass a function that returns whether it should remove the item.

In your case, if you had:

var thingA = { word: item, type: itemType };
var thingB = { word: item, type: ItemType };
var thingC = thingA;

thingA and thingB would be two different objects that are not equal to each other.  thingC would be a reference to the same object as thingA.

So, you can either pass the exact reference to your object or a function like:

viewModel.adj.remove(function(item) {
  return item.word === item && item.type === itemType;
});

This would remove any items that match the word and type specified. 

Hope that helps.




On Wednesday, May 29, 2013 7:48:35 PM UTC-5, Lee Blazek wrote:

Ryan Rahlf

unread,
May 29, 2013, 10:14:41 PM5/29/13
to knock...@googlegroups.com
Jinx!  :-D
Reply all
Reply to author
Forward
0 new messages