简体   繁体   English

如何按对象从对象常量数组中删除对象常量

[英]How to remove an object literals from an array of object literals by object

What is the most general methodology (possibly in javascript) to remove an "object literal" from an array of "objects literal", I mean how to determine what the index of the object to be removed is. 什么是从“对象文字”数组中删除“对象文字”的最通用方法(可能是在javascript中),我的意思是如何确定要删除的对象的索引是什么。

The keys are the same, only the values can change. 键是相同的,只有值可以更改。

Lets suppose we have 假设我们有

arrObj = [{k: "a"},{k: "b"},{k: "c"},{k: "d"}]; //{k: "a"}

is just an example of oject literals, but each object could be also more nested like this 只是对象文字的一个示例,但是每个对象也可以像这样嵌套更多

{k: "a", {y: {z: "c"}} }

I want to define a function literal which is able to make this stuff: 我想定义一个函数文字,它可以使这些东西:

var removeObjFromArray = function (obj){ ... };    
arrObj.removeObjFromArray({k: "b"}); // {k: "a"},{k: "c"},{k: "d"}

There's probably a much better way of doing whatever it is you're doing. 无论您正在做什么,可能都有更好的方法。 To achieve the solution you want here, you need to iterate over the array and then compare the object. 为了实现您想要的解决方案,您需要遍历数组, 然后比较对象。 One solution is to use JSON, although you would see a performance hit in older browsers that need the compatibility shim . 一种解决方案是使用JSON,尽管在需要兼容性shim的旧版浏览器中,您会发现性能下降 This is only a viable solution if you don't need to compare functions. 如果您不需要比较功能,这只是一个可行的解决方案。 Something like this should work: 这样的事情应该起作用:

function removeFromArray(arr, obj) {
    obj = JSON.stringify(obj);
    for (var i=0, max = arr.length; i < max; i++) {
        if (JSON.stringify(arr[i]) === obj)
            arr.splice(i, 1);
    }
}

Working example: http://jsfiddle.net/4JmzV/ 工作示例: http : //jsfiddle.net/4JmzV/

Note that, as cdhowie mentions in the comments, order of iteration is a factor with JSON and object properties would need to be defined in the same order. 请注意,正如cdhowie在评论中提到的那样,迭代顺序是JSON的一个因素,并且对象属性需要以相同顺序定义。 However, without JSON, you'd have to iterate over each object's keys and iterate over the object passed in the argument several times to ensure both objects contain the same keys and same values, making it a more complex task and much slower vs native JSON. 但是,如果没有JSON,则必须遍历每个对象的键遍历传入参数的对象数次,以确保两个对象都包含相同的键和相同的值,这使其变得更加复杂,并且与本机JSON相比要慢得多。

As I said in my first sentence, "There's probably a much better way of doing whatever it is you're doing" , and you might be better off taking another look at your approach. 正如我在第一句话中所说的: “可能有更好的方式来做您正在做的事情” ,而您最好再看看您的方法。

You need to define when two objects are "the same". 您需要定义两个对象何时“相同”。 In your example, the argument to removeObjFromArray is actually a different object (if compared by reference) than the object in the array, since each literal syntax creates a distinct object. 在您的示例中, removeObjFromArray的参数实际上是与数组中的对象不同的对象(如果通过引用进行比较),因为每种文字语法都会创建一个不同的对象。

If you know they always have a key named "k", you can compare on that, but I guess you are looking for a more general solution? 如果您知道他们总是有一个名为“ k”的密钥,您可以对此进行比较,但是我想您正在寻找更通用的解决方案?

You could decide that two objects are the same if they have the same properties with the same values (recursively compared in the case of objects). 如果两个对象具有相同的属性且具有相同的值,则可以确定两个对象是相同的(如果是对象,则进行递归比较)。 But it really depends on the uses cases for your program. 但这实际上取决于程序的用例。

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

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