Javascript如何比较数组中的第一个元素与其他元素

时间:2016-11-25 10:10:41

标签: javascript arrays

我有一个数组mainArray,其中包含n个对象数组,我需要比较mainArray中的所有元素,以检查每个对象的属性id是否存在于其他对象:
- 如果找到,则检查其他属性是否相同,如果它们不相同则返回id - 如果mainArray[n]中至少有一个没有id的对象,则返回id

以下是一些例子:
例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]没有具有属性"id": "evar9"

的对象

示例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]没有属性"id": "trackingcode"的对象 - mainArray[0]mainArray[1]没有属性"id": "evar1"的对象 - mainArray[1]mainArray[2]没有包含属性"id": "evar9"

的对象

示例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
            }
          ]
        }
      ]
    }
  ]
]

返回[“evar9”]
- "children"属性不同

1 个答案:

答案 0 :(得分:1)

您可以迭代并计算具有相同名称的对象,并检查是否找到了相等的id

此提案使用JSON.stringify,因为我认为生成的数据相同且属性的顺序相同。

/**
 * 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; }