0

Not quite sure how to phrase this question (I am sure it has been asked before in some form).

My problem is essentially shown below (through some badly written pseudo-javascript code):

var list = []
for (i to somenumber)
    list.push(new myObject);

list.foreach(function(item){
    if (item.name === 'WhatIAmLookingFor')
        item.delete() <--- This needs to remove the object from list
}

So as my amazing code alludes to I want to be able to remove the item from the list by calling a function on an object in the list.

Sorry if this is an ignorant question but I cant figure out how to do this.

Rabid
  • 316
  • 4
  • 14
  • Try delete item instead of item.delete(). See here: http://stackoverflow.com/questions/208105/how-to-remove-a-property-from-a-javascript-object. BTW just one google search was necessary – toesslab Sep 18 '14 at 15:18
  • Do you mean [this](http://stackoverflow.com/a/11058411)? – chridam Sep 18 '14 at 15:20
  • 1
    Not a dupe: they're asking how to remove an object with specific properties, not a given primitive. `indexOf` answers are useless here. – georg Sep 18 '14 at 15:25
  • As georg said. Not a primitive. And I may be wrong here (new to javascript and scoping is a bit confusing to me) but the array does not exist within the scope of the item in the array. So I cant search the array from the object. – Rabid Sep 18 '14 at 15:27
  • A function has a scope, an object not. Your items are objects. If you replace your `forEach`(uppercase E!)-function with georg 's filter-function, it should work. It's a clean solution you should **try**. You may come back and tell what went wrong. – Martin Ernst Sep 18 '14 at 18:10

1 Answers1

2

Instead of deleting items, use filter to keep only "good" ones.

var list = [
  { name: 'foo' },
  { name: 'bar' },  
  { name: 'removeMe' },  
  { name: 'baz' }
];

list = list.filter(function(item) {
  return item.name != 'removeMe'
});

document.write(JSON.stringify(list))

To remove an element "inside out", e.g. element.removeFrom(list), you need array.splice:

obj = function(name) {
  
  this.name = name;
  
  this.removeFrom = function(lst) {
    for (var i = 0; i < lst.length; ) {
     if (lst[i].name == this.name)
          lst.splice(i, 1);
        else
          i++;
    }
  }
}
  
a = new obj('a');
b = new obj('b');
c = new obj('c');
x = new obj('remove');

list = [a, b, c, x, x, a, b, c, x]

x.removeFrom(list)

document.write(JSON.stringify(list))
georg
  • 195,833
  • 46
  • 263
  • 351
  • Can this be done from within the objects scope? I want to be calling one of the objects public methods to remove it from the list. But the array wont be within that functions scope. – Rabid Sep 18 '14 at 15:32
  • @Rabid: like `someObj.removeMe(someList)`? – georg Sep 18 '14 at 15:40
  • That was what I was thinking. But if I provide the array as a parameter to the function will it be passed by reference or value? Can you pass by reference in javascript? – Rabid Sep 19 '14 at 06:21
  • @Rabid: javascript is "pass by object value" - you pass a value, but this value is a pointer to an object. Therefore, you can use mutator methods, like `splice`, on it - see the update. – georg Sep 19 '14 at 08:05
  • Thanks. Thats what I was looking for! – Rabid Sep 19 '14 at 08:14