I want to compare two array of objects, they should be considered equal if their elements are same but not in same order. e.g. [{a:1}, {b:2}] and [{b:2}, {a:1}]
I am using lodash-v3's isEqual which can compare two values but it only gives true if the order in array is same, so I've implement one function that compares elements recursively.
function deepEqual(data1, data2) {
  data1 = _.cloneDeep(data1);
  data2 = _.cloneDeep(data2);
  function isDeepEqual(val1, val2) {
    if (_.isArray(val1) && _.isArray(val2)) {
      if (val1.length === val2.length) {
        for (let id1 in val1) {
          let hasMatch = false;
          let matchOnIndex = -1;
          for (let id2 in val2) {
            if (isDeepEqual(val1[id1], val2[id2])) {
              hasMatch = true;
              matchOnIndex = id2;
              break;
            }
          }
          if (hasMatch) {
            val2.splice(matchOnIndex, 1);
          } else {
            return false;
          }
        }
        return true;
      } else {
        return false;
      }
    }
    if (_.isPlainObject(val1) && _.isPlainObject(val2)) {
      if (Object.keys(val1).length === Object.keys(val2).length) {
        for (let temp1 in val1) {
          if (!isDeepEqual(val1[temp1], val2[temp1])) {
            return false;
          }
        }
        return true;
      } else {
        return false;
      }
    }
    return _.isEqual(val1, val2);
  }
  return isDeepEqual(data1, data2);
}
Above function works, but how can i improve it performance wise? If there is any simple implementation with lodash3 that works for me as well.
Link to above function's fiddle.
EDIT:
The two array of objects can be nested, e.g.
[{
  a:1,
  b:[{
    c: [1, 2]
  },
  {
    d: [3, 4]
  }]
},{
  e:1,
  f:[{
    g: [5, 6]
  },
  {
    h: [7, 8]
  }]
}]
and
[{
  e:1,
  f:[{
    h: [8, 7]
  },{
    g: [6, 5]
  }]
},{
  a:1,
  b:[{
    d: [4, 3]
  },{
    c: [2, 1]
  }]
}]
Arrays can also not have unique values(as users are creating this arrays).
This might be possible with _.isEqualWith as @Koushik and @tokland suggested. Unfortunately it's available from lodashv4 so I can't use it.
Similar solution is also mentioned in this comment.
Really sorry for not clearly specifying the examples. The fiddle has all different type of cases.
 
     
     
    