简体   繁体   English

搜索 JavaScript 对象列表并确定它们是否包含相同值的最有效方法是什么?

[英]What is the most efficient way to search through a list of JavaScript objects and determine whether they contain the same values?

I have the following function which takes some values that the user has entered and creates JavaScript objects from those values, then puts those objects in an array and returns the array:我有以下函数,它接受用户输入的一些值并从这些值创建 JavaScript 对象,然后将这些对象放入一个数组中并返回该数组:

function createObjects() {
    var objectArray = []; 
    for (var i = 0; i < someCount; i++) {
        var object = {
            property1: someProperty1, 
            property2: someProperty2,
            property3: someProperty3,
            property4: someProperty4
        };
        objectArray.push(object);
    }
    return objectArray;
}

Now, I want to compare these objects' properties and determine whether any two contain all of the same values for property1 , property2 , property3 , and property4 .现在,我想比较这些对象的属性并确定是否有任何两个对象包含property1property2property3property4所有相同值。 If any two of these objects have all four of the same values for these properties, I want a validation check to return false .如果这些对象中的任何两个具有这些属性的所有四个相同值,我希望验证检查返回false Here is what I have so far:这是我到目前为止所拥有的:

function objectsAreUnique() {
    var objects = createObjects(); 
    for(var i = 0; i < objects.length; i++) {
        //need to determine whether all four of the properties are the same for any two objects
        //if(objectsAreSame) { return false; } 
    }
    return true; 
}

I have a few ideas, but I'm interested to see what is the most efficient way to achieve this.我有一些想法,但我很想知道实现这一目标的最有效方法是什么。 Thanks!谢谢!

If you can guarantee that the properties will always be inserted in the same order (which will be the case if using an object literal as in your example), you can do this in ~O(n) using JSON.stringify and a Set :如果您可以保证始终以相同的顺序插入属性(如果使用您的示例中的对象文字就是这种情况),您可以使用JSON.stringifySet在 ~O(n) 中执行此操作:

function objectsAreUnique() {
    const objects = createObjects();
    return (new Set(objects.map(o => JSON.stringify(o)))).size == objects.length;
}
function objectsAreUnique() {
    var objects = createObjects(); 
    var stringifiedAndSorted = objects.map(obj => JSON.stringify(obj)).sort()
    for(var i = 0; i < stringifiedAndSorted.length-1; i++) {
        if(i === i+1) 
           return false;
    }
    return true; 
}

First, create a function to test whether two objects are the same.首先,创建一个函数来测试两个对象是否相同。 You can enter each property individually, or get creative with the JSON.stringify function您可以单独输入每个属性,也可以使用 JSON.stringify 函数发挥创意

properties individually:单独属性:

function objectsIdentical(obj1, obj2) {
    return obj1.property1 == obj2.property1 && obj1.property2 == obj2.property2 && obj1.property3 == obj2.property3 && obj1.property4 == obj2.property4;
}

JSON.stringify (recommended for objects with many properties) JSON.stringify(推荐用于具有许多属性的对象)

function objectsIdentical(obj1, obj2) {
    return JSON.stringify(obj1).replace(/^.|.$/g, "").split(",").sort().join(",") == JSON.stringify(obj2).replace(/^.|.$/g, "").split(",").sort().join(",");
}    

Then, you can use a for loop to check if any of them are identical.然后,您可以使用 for 循环来检查它们中的任何一个是否相同。

for (let i=0; i<objects.length-1; i++) {
    for (let j=i+1; j<objects.length; j++) {
        if (objectsIdentical(objects[i], objects[j])) {
            return false;
        }
    }
}
return true;

If you're familiar with the "some" function, you can use that.如果您熟悉“some”函数,则可以使用它。

return !objects.some((v, i) => objects.slice(i+1).some(w => objectsIdentical(v, w)))

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

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