2

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 comments, 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.

7
  • 1
    Are you sure your list is an object not an array? Commented Aug 6, 2014 at 23:47
  • You are correct, it is an array of objects. Commented Aug 6, 2014 at 23:48
  • 1
    Then your array definition in the question is broken. 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 ? 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 Commented Aug 7, 2014 at 0:23

5 Answers 5

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);
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

1

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

Comments

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

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

removeProperty(list,"1");

console.log(list);

9 Comments

Uhm, why do you need loop here? if (x in obj) { delete ... }
Yep :-) But the thing is that OP has an array :-)
OP's list is not valid. If it is made into an object like var list = {1: [{},{}], 2: [{},{}] }, delete should work.
@RajkumarMadhuram Did OP edit his question? I copied that list variable from the question.
Yep they did, unsuccessfully though )
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.