简体   繁体   English

从对象的 javascript 列表中删除对象

[英]Removing an object from a javascript list of objects

I currently have a list of objects in javascript indexed by a key:我目前在 javascript 中有一个由键索引的对象列表:

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)我会列出删除基于键的条目 (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?关于如何删除 javascript 或 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.感谢所有的反馈。

I'll assume list is an object, not an array. 我假设list是一个对象,而不是一个数组。

If you want to reset a or (or b it's done the same way) 如果您想重置a或(或b它做了同样的方式)

list.a.length = 0;

If you want to delete an element from a at a known index (let index ) 如果你想从删除元素a已知指数(让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). 您可以通过检查list.length的值来验证这list.length (将为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. 这将允许您使用delete并使其行为符合您的预期。 Of course you'll lose the .length property on the array but you never had that anyway. 当然你会丢失阵列上的.length属性但是你从来没有这样做过。

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) 我会列出基于密钥删除条目(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: 意味着您要根据键(a / b)选择要考虑的列表,然后删除列表中的元素(或所有这些元素),您可以尝试这样做:

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). 我做了几个不同情况的Plunker (检查script.js文件)。 There seems to be a bit of confusion on what you are after and hopefully this is helpful to you somehow. 对你所追求的东西似乎有点混乱,希望这对你有所帮助。 Good luck. 祝好运。

Create a simple prototype for the Array class为 Array 类创建一个简单的原型

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上面的代码会将remove方法添加到您的所有列表中,因此这不仅可以帮助您处理对象,还可以帮助您处理字符串、整数或任何数据类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM