1

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

2
  • why not 'evar1' as well at the last question? Commented Nov 25, 2016 at 10:42
  • 1
    @NinaScholz sorry, I thought that I removed that object. Check now. Commented Nov 25, 2016 at 10:47

1 Answer 1

1

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.

/**
 * Checks every array for sameness and returns only different id
 *
 * @param {array} array An array with nested arrays with object, with property id
 * @return Difference.
 */
function check(array) {

    // create object without prototypes
    var count = Object.create(null);

    // iterate given array
    array.forEach(function (a, i) {
   
        // and the inner array
        a.forEach(function (b, j) {

            // check if id exist, if not take a new object with count,
            // objects and same for later check
            count[b.id] = count[b.id] || { count: 0, objects: [], same: true };

            // increment count for later check with the length of the given array
            count[b.id].count++;

            // save actual object for check for sameness
            count[b.id].objects.push(b);

            // if count is greater than one, test obejct for equality
            if (count[b.id].count > 1) {

                // assign to property same result of test with serialized objects
                count[b.id].same = count[b.id].same && JSON.stringify(count[b.id].objects[0]) === JSON.stringify(b);
            }
        });
    });

    // get all keys and return only the keys which count is not equal 
    // to the lenght of the given array or which objects are not equal
    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; }

Sign up to request clarification or add additional context in comments.

2 Comments

This works perfect, thank you! I need to study the code a little to understand what happens there...it would help me a lot if you can add just a couple comments in code...
please see edit, i change the handling form indices to objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.