Pavel Valeriu Pavel Valeriu - 15 days ago 7
Javascript Question

Javascript how to compare the first element from array with the other elements

I have an array

mainArray
that contains
n
arrays of objects and I need to compare all elements from
mainArray
to check if the property
id
from each object exists in the other objects:

- if it is found, then check if the other properties are identical and return the
id
if they are not identical.

- if at least one of the
mainArray[n]
does not have an object with that
id
then return the
id
.

Here are some examples:

Example 1:

mainArray = [
[
{
"id":"trackingcode",
"classification":
[
{
"name":"Creative Elements",
"description":"",
"type":"text",
"date_enabled":false
},
{
"name":"Campaigns",
"description":"",
"type":"text",
"date_enabled":true
}
]
},
{
"id":"evar9",
"classification":
[
{
"name":"Type",
"description":"",
"type":"text",
"date_enabled":false
},
{
"name":"Domain",
"description":"",
"type":"text",
"date_enabled":false,
"children":
[
{
"name":"sub classification",
"description":"",
"type":"text",
"parent_name":"Domain",
"date_enabled":false
}
]
}
]
}
],
[
{
"id":"trackingcode",
"classification":
[
{
"name":"Creative Elements",
"description":"",
"type":"text",
"date_enabled":false
},
{
"name":"Campaigns",
"description":"",
"type":"text",
"date_enabled":true
}
]
}
]
]


mainArray[0] !== mainArray[1] => return ["evar9"]

- mainArray[1] does not have an object with the property
"id": "evar9"


Example 2:

mainArray = [
[
{
"id":"evar9",
"classification":
[
{
"name":"Type",
"description":"",
"type":"text",
"date_enabled":false
},
{
"name":"Domain",
"description":"",
"type":"text",
"date_enabled":false,
"children":
[
{
"name":"sub classification",
"description":"",
"type":"text",
"parent_name":"Domain",
"date_enabled":false
}
]
}
]
}
],
[
{
"id":"trackingcode",
"classification":
[
{
"name":"Creative Elements",
"description":"",
"type":"text",
"date_enabled":false
},
{
"name":"Campaigns",
"description":"",
"type":"text",
"date_enabled":true
}
]
}
],
[
{
"id":"trackingcode",
"classification":
[
{
"name":"Creative Elements",
"description":"",
"type":"text",
"date_enabled":false
},
{
"name":"Campaigns",
"description":"",
"type":"text",
"date_enabled":true
}
]
},
{
"id":"evar1",
"classification":
[
{
"name":"Creative",
"description":"",
"type":"text",
"date_enabled":false
},
{
"name":"Variables",
"description":"",
"type":"text",
"date_enabled":true
}
]
}
]
]


return ["trackingcode", "evar1", "evar9"]

-
mainArray[0]
does not have an object with the property
"id": "trackingcode"


-
mainArray[0]
and
mainArray[1]
does not have an object with the property
"id": "evar1"


-
mainArray[1]
and
mainArray[2]
does not have an object with the property
"id": "evar9"


Example 3:

mainArray = [
[
{
"id":"trackingcode",
"classification":
[
{
"name":"Creative Elements",
"description":"",
"type":"text",
"date_enabled":false
},
{
"name":"Campaigns",
"description":"",
"type":"text",
"date_enabled":true
}
]
},
{
"id":"evar9",
"classification":
[
{
"name":"Type",
"description":"",
"type":"text",
"date_enabled":false
},
{
"name":"Domain",
"description":"",
"type":"text",
"date_enabled":false,
"children":
[
{
"name":"sub classification",
"description":"",
"type":"text",
"parent_name":"Domain",
"date_enabled":false
}
]
}
]
}
],
[
{
"id":"trackingcode",
"classification":
[
{
"name":"Creative Elements",
"description":"",
"type":"text",
"date_enabled":false
},
{
"name":"Campaigns",
"description":"",
"type":"text",
"date_enabled":true
}
]
},
{
"id":"evar9",
"classification":
[
{
"name":"Type",
"description":"",
"type":"text",
"date_enabled":false
},
{
"name":"Domain",
"description":"",
"type":"text",
"date_enabled":false,
"children":
[
{
"name":"different name",
"description":"different description",
"type":"text",
"parent_name":"Domain",
"date_enabled":false
}
]
}
]
}
]
]


return ["evar9"]

- the
"children"
properties are different

Answer

You could iterate and count the objects with the same name and make a check if some equal id are found.

This proposal uses JSON.stringify, because I think the data is equal generated and the order of properties is the same.

function check(array) {
    var count = Object.create(null);
    array.forEach(function (a, i) {
        a.forEach(function (b, j) {
            count[b.id] = count[b.id] || { count: 0, indices: [] };
            count[b.id].count++;
            count[b.id].indices.push([i, j]);
            if (count[b.id].count > 1) {
                count[b.id].same = JSON.stringify(array[count[b.id].indices[0][0]][count[b.id].indices[0][1]]) === JSON.stringify(b);
            }
        });
    });
    return Object.keys(count).filter(function (k) {
        return count[k].count !== array.length || !count[k].same;
    });
}

var mainArray1 = [[{ "id": "trackingcode", "classification": [{ "name": "Creative Elements", "description": "", "type": "text", "date_enabled": false }, { "name": "Campaigns", "description": "", "type": "text", "date_enabled": true }] }, { "id": "evar9", "classification": [{ "name": "Type", "description": "", "type": "text", "date_enabled": false }, { "name": "Domain", "description": "", "type": "text", "date_enabled": false, "children": [{ "name": "sub classification", "description": "", "type": "text", "parent_name": "Domain", "date_enabled": false }] }] }], [{ "id": "trackingcode", "classification": [{ "name": "Creative Elements", "description": "", "type": "text", "date_enabled": false }, { "name": "Campaigns", "description": "", "type": "text", "date_enabled": true }] }]],
    mainArray2 = [[{ "id": "evar9", "classification": [{ "name": "Type", "description": "", "type": "text", "date_enabled": false }, { "name": "Domain", "description": "", "type": "text", "date_enabled": false, "children": [{ "name": "sub classification", "description": "", "type": "text", "parent_name": "Domain", "date_enabled": false }] }] }], [{ "id": "trackingcode", "classification": [{ "name": "Creative Elements", "description": "", "type": "text", "date_enabled": false }, { "name": "Campaigns", "description": "", "type": "text", "date_enabled": true }] }], [{ "id": "trackingcode", "classification": [{ "name": "Creative Elements", "description": "", "type": "text", "date_enabled": false }, { "name": "Campaigns", "description": "", "type": "text", "date_enabled": true }] }, { "id": "evar1", "classification": [{ "name": "Creative", "description": "", "type": "text", "date_enabled": false }, { "name": "Variables", "description": "", "type": "text", "date_enabled": true }] }]],
    mainArray3 = [[{ "id": "trackingcode", "classification": [{ "name": "Creative Elements", "description": "", "type": "text", "date_enabled": false }, { "name": "Campaigns", "description": "", "type": "text", "date_enabled": true }] }, { "id": "evar9", "classification": [{ "name": "Type", "description": "", "type": "text", "date_enabled": false }, { "name": "Domain", "description": "", "type": "text", "date_enabled": false, "children": [{ "name": "sub classification", "description": "", "type": "text", "parent_name": "Domain", "date_enabled": false }] }] }], [{ "id": "trackingcode", "classification": [{ "name": "Creative Elements", "description": "", "type": "text", "date_enabled": false }, { "name": "Campaigns", "description": "", "type": "text", "date_enabled": true }] }, { "id": "evar9", "classification": [{ "name": "Type", "description": "", "type": "text", "date_enabled": false }, { "name": "Domain", "description": "", "type": "text", "date_enabled": false, "children": [{ "name": "different name", "description": "different description", "type": "text", "parent_name": "Domain", "date_enabled": false }] }] }]];

console.log(check(mainArray1));
console.log(check(mainArray2));
console.log(check(mainArray3));
.as-console-wrapper { max-height: 100% !important; top: 0; }