如何在键值对数组sails.js中查找重复项

时间:2022-06-09 07:37:31

I have an array as below.

我有一个数组如下。

[emails] => Array (
   [0] => Array (
      [a] => a@a.com
      [is_verified_a] => 1
    )
   [1] => Array (
      [b] => a@a.com
      [is_verified_b] => 1
   )
),

I need to find duplicates in this array , but keys are different ie a and b have the same email 'a@a.com'.

我需要在这个数组中找到重复项,但键是不同的,即a和b有相同的电子邮件'a@a.com'。

ie , Have to check for duplicates in this array and need only the value 'a@a.com' in a separate variable. Is there any way to find out using _.uniq or any other underscore functions?

即,必须检查此数组中的重复项,并且在单独的变量中只需要值“a@a.com”。有没有办法找出使用_.uniq或任何其他下划线功能?

Please help.Thanks a lot.

请帮帮忙。谢谢。

1 个解决方案

#1


1  

What you want to do is not clear. However since you're using JavaScript your array should look like:

你想做的事情并不清楚。但是,由于您使用的是JavaScript,因此您的数组应如下所示:

var emails = [
    {
        email: "a@a.com",
        verified: true,
        key: "a"
    },
    {
        email: "a@a.com",
        verified: true,
        key: "b"
    }
];

It makes no sense to encode keys in your objects separately (especially not in property names).

单独编码对象中的键是没有意义的(特别是在属性名称中)。

To remove duplicates from an array we can create a generic removeDuplicatesBy function:

要从数组中删除重复项,我们可以创建一个通用的removeDuplicatesBy函数:

function removeDuplicatesBy(comparator, array) {
    var length = array.length;
    var unique = [];

    for (var i = 0; i < length; i++) {
        var element = array[i];
        var isUnique = true;

        for (var j = 0; j < i; j++) {
            if (comparator(element, array[j])) {
                isUnique = false;
                break;
            }
        }

        if (isUnique) unique.push(element);
    }

    return unique;
}

Now we can remove duplicates in the emails array as follows:

现在我们可以删除电子邮件数组中的重复项,如下所示:

var uniqueEmails = removeDuplicatesBy(function (a, b) {
    return a.email === b.email && a.verified === b.verified;
}, emails);

If you need to maintain keys in your objects then you're probably doing something wrong. You should consider restructuring your code.

如果您需要在对象中维护密钥,那么您可能做错了什么。您应该考虑重构代码。

#1


1  

What you want to do is not clear. However since you're using JavaScript your array should look like:

你想做的事情并不清楚。但是,由于您使用的是JavaScript,因此您的数组应如下所示:

var emails = [
    {
        email: "a@a.com",
        verified: true,
        key: "a"
    },
    {
        email: "a@a.com",
        verified: true,
        key: "b"
    }
];

It makes no sense to encode keys in your objects separately (especially not in property names).

单独编码对象中的键是没有意义的(特别是在属性名称中)。

To remove duplicates from an array we can create a generic removeDuplicatesBy function:

要从数组中删除重复项,我们可以创建一个通用的removeDuplicatesBy函数:

function removeDuplicatesBy(comparator, array) {
    var length = array.length;
    var unique = [];

    for (var i = 0; i < length; i++) {
        var element = array[i];
        var isUnique = true;

        for (var j = 0; j < i; j++) {
            if (comparator(element, array[j])) {
                isUnique = false;
                break;
            }
        }

        if (isUnique) unique.push(element);
    }

    return unique;
}

Now we can remove duplicates in the emails array as follows:

现在我们可以删除电子邮件数组中的重复项,如下所示:

var uniqueEmails = removeDuplicatesBy(function (a, b) {
    return a.email === b.email && a.verified === b.verified;
}, emails);

If you need to maintain keys in your objects then you're probably doing something wrong. You should consider restructuring your code.

如果您需要在对象中维护密钥,那么您可能做错了什么。您应该考虑重构代码。