jquery - Removing an object from a javascript list of objects - Stack Overflow

I currently have a list of objects in javascript indexed by a key:var list = [];list['a'] =

I currently have a list of objects in javascript indexed by a key:

var list = [];
list['a'] = [];
list['a'].push({obj: 'test'});
list['a'].push({obj: 'test2'});

list['b'] = [];
list['b'].push({obj: 'test'});
list['b'].push({obj: 'test2'});

I would list to remove the entry based on the key (a/b)

I have tried the following:

for(var x in list) { delete list[x]; }

that works but it actually leaves an undefined entry in the list.

I have also tried splicing the array, but that does not seems to work in this case.

Any thoughts on how to remove the entry in javascript or jQuery?

Thanks.

The Fix:

After reading some of the ments, i was able to better understand what my list is consistent of. Therefor, i was able to do the removal by doing the following:

delete list.b;

I'm not sure if my list is best way to organize my structure, but doing a delete on the list and treating it like an object property did the trick.

Thanks for all the feedback.

I currently have a list of objects in javascript indexed by a key:

var list = [];
list['a'] = [];
list['a'].push({obj: 'test'});
list['a'].push({obj: 'test2'});

list['b'] = [];
list['b'].push({obj: 'test'});
list['b'].push({obj: 'test2'});

I would list to remove the entry based on the key (a/b)

I have tried the following:

for(var x in list) { delete list[x]; }

that works but it actually leaves an undefined entry in the list.

I have also tried splicing the array, but that does not seems to work in this case.

Any thoughts on how to remove the entry in javascript or jQuery?

Thanks.

The Fix:

After reading some of the ments, i was able to better understand what my list is consistent of. Therefor, i was able to do the removal by doing the following:

delete list.b;

I'm not sure if my list is best way to organize my structure, but doing a delete on the list and treating it like an object property did the trick.

Thanks for all the feedback.

Share Improve this question edited Aug 7, 2014 at 17:58 Dani asked Aug 6, 2014 at 23:44 DaniDani 5,9082 gold badges19 silver badges21 bronze badges 7
  • 1 Are you sure your list is an object not an array? – zerkms Commented Aug 6, 2014 at 23:47
  • You are correct, it is an array of objects. – Dani Commented Aug 6, 2014 at 23:48
  • 1 Then your array definition in the question is broken. – zerkms Commented Aug 6, 2014 at 23:49
  • @Dani Do you want to reset a or b, or do you want to remove an element from a or b ? – axelduch Commented Aug 7, 2014 at 0:16
  • information has been so badly put together here that confusion is abounding. Latest code isn't an array at all regardless if you initially define it as one. list['a'] makes it an object since javascript has no associative arrays – charlietfl Commented Aug 7, 2014 at 0:23
 |  Show 2 more ments

5 Answers 5

Reset to default 2

I'll assume list is an object, not an array.

If you want to reset a or (or b it's done the same way)

list.a.length = 0;

If you want to delete an element from a at a known index (let index)

list.a.splice(index, 1);

You're attempting to add the elements to the array object as object properties and not as array elements. You can verify this by inspecting the value of list.length (will be 0).

So when doing something such as the following:

function removeProperty(id) {
    if (list.hasOwnProperty(id)) {
      delete list[id];
  }
}

removeProperty('a');

it's really the same as:

delete list.a;

which is why you think it leaves an undefined 'entry' in the 'list'.

You'll need to use a literal object instead:

var list = {};

list['a'] = [];
...

list['b' = [];
...

which would allow you to use delete and have it behave as you expect. Of course you'll lose the .length property on the array but you never had that anyway.

Create a simple prototype for the Array class

Array.prototype.remove = function() {
    // Helper function to remove a single element from a list if exists
    item = arguments[0]
    if (this.includes(item)) {
        index = this.indexOf(item)
        this.splice(index, 1)
     }
}

// Now we can call
myList.remove(YourObject)

The above code will add the remove method to all your lists, so this will help you not just for objects but also strings, integers, or any data type

var list = {1: [{},{}], 2: [{},{}]};

function removeProperty(obj, prop){
    if(obj[prop]){
        delete obj[prop];
    }
}

removeProperty(list,"1");

console.log(list);

If this quote:

I would list to remove the entry based on the key (a/b)

means you would like to select the list to consider based off the key (a/b), then remove elements in the list (or all of them), you can try this:

var list = [];
list['a'] = [];
list['a'].push({obj: 'test4'});
list['a'].push({obj: 'test5'});

list['b'] = [];
list['b'].push({obj: 'test'});
list['b'].push({obj: 'test2'});

var toRemove = 'test4';
var removeFrom = "a";

var consideredList;
for (var prop in list) {
  if (prop == removeFrom) {
    consideredList = list[prop];
  }
}

//Remove everything from the considered list
consideredList.splice(0, consideredList.length);

//Remove based off value, if you know the property  name
// for(var pos in consideredList) {
//   if(consideredList[pos].obj == toRemove) {
//     consideredList.splice(pos, 1);
//   }
// }

I made a Plunker of a few different cases (check the script.js file). There seems to be a bit of confusion on what you are after and hopefully this is helpful to you somehow. Good luck.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744236229a4564482.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信