-1

i have two arrays of objects which i want to compare . the arrays are equals if they have the same objects even not in the same order . For example :

  [
   { name: 'Lisa', value: '25', height: 165 },
   { name: 'jack',  value: '12 ',  height: 190},
   { name: 'jhon', value: '50', height: 176 }
  ]
  
    //and

  [
   { name: 'jhon', value: '50', height: 176 },
   { name: 'Lisa', value: '25', height: 165 },
   { name: 'jack',  value: '12 ',  height: 190}
  ]  
   //equals 

are equals but

  [
   { name: 'Lisa', value: '25', height: 165 },
   { name: 'jack',  value: '12 ',  height: 190},
   { name: 'jhon', value: '50', height: 176 }
   
  ]
  
    //and

  [
   { name: 'jhon', value: '50', height: 176 },
   { name: 'Lisa', value: '25', height: 165 },
   { name: 'jack',  value: '12 ',  height: 190},
   { name: 'Mark', value: '50', height: 140 }
  ]
      //not equal

and

   [
    { name: 'Lisa', value: '25', height: 165 },
    { name: 'jack',  value: '12 ',  height: 190},
    { name: 'jhon', value: '50', height: 176 }

  ]

//and

[
 { name: 'jhon', value: '50' },
 { name: 'Lisa', value: '25', height: 165 },
 { name: 'jack',  value: '12 ',  height: 190},

]
//not equals 

the two arrays in the last example are not equals because they don't have exaclty the same objects . I tried many things that there is no need to show because i'm not even close since i am beginner to javascript thank you for your help :)

Ghost
  • 57
  • 1
  • 7
  • Show us what you have tried. SO isn't a free code writing service. The objective here is for you to post your attempts to solve your own issue and others help when they don't work as expected. See [ask] and [mcve] – charlietfl Mar 24 '21 at 23:29
  • 1
    Sort the two arrays by the name (assuming names are unique), then compare the corresponding objects in the two arrays. See https://stackoverflow.com/questions/201183/how-to-determine-equality-for-two-javascript-objects for how to compare objects. – Barmar Mar 24 '21 at 23:31
  • 1
    I think you can first check whether the 2 arrays have the same length (if not same length, then definitely not equal).. if length are equal, you can iterate the first array and for each element in the first array use second array.find() – wakakak Mar 24 '21 at 23:31
  • Once array length and sort is done, use `Array.prototype.every()` – Bibberty Mar 24 '21 at 23:33

2 Answers2

0

Here, check length, then sort and compare.

const arr1 = [
   { name: 'Lisa', value: '25', height: 165 },
   { name: 'jack',  value: '12 ',  height: 190},
   { name: 'jhon', value: '50', height: 176 }
  ];
const arr2 =   [
   { name: 'jhon', value: '50', height: 176 },
   { name: 'Lisa', value: '25', height: 165 },
   { name: 'jack',  value: '12 ',  height: 190}
  ]; 
const arr3 =  [
   { name: 'jhon', value: '50', height: 176 },
   { name: 'Lisa', value: '25', height: 165 },
   { name: 'jack',  value: '12 ',  height: 190},
   { name: 'Mark', value: '50', height: 140 }
  ];
const arr4 =   [
   { name: 'jhon', value: '50', height: 176, extraField: 1 },
   { name: 'Lisa', value: '25', height: 165 },
   { name: 'jack',  value: '12 ',  height: 190}
  ]; 
  
const sortByName = (arr) => arr.sort(({ name: a}, {name: b}) => (a > b) ? 1 : (a < b) ? -1 : 0 ); 

const objEqual = (objA, objB) => {
  const keys = [...new Set(Object.keys(objA).concat(Object.keys(objB)))];
  if (keys.length !== Object.keys(objA).length) return false;
  return keys.every(k => objA[k] === objB[k]);
}

const areEqual = (arrA, arrB) => {
  if (arrA.length !== arrB.length) return false;
  const a = sortByName(arrA);
  const b = sortByName(arrB);
  
  return a.every((obj, i) => objEqual(obj, b[i]));
};

console.log(areEqual(arr1, arr2));
console.log(areEqual(arr1, arr3));
console.log(areEqual(arr1, arr4));

Another option after testing Length. Is to use a Set.

const arr1 = [
   { name: 'Lisa', value: '25', height: 165 },
   { name: 'jack',  value: '12',  height: 190},
   { name: 'jhon', value: '50', height: 176 }
  ];
const arr2 =   [
   { name: 'jhon', value: '50', height: 176 },
   { name: 'Lisa', value: '25', height: 165 },
   { name: 'jack',  value: '12',  height: 190}
  ]; 
const arr3 =  [
   { name: 'jhon', value: '50', height: 176 },
   { name: 'Lisa', value: '25', height: 165 },
   { name: 'jack',  value: '12',  height: 190},
   { name: 'Mark', value: '50', height: 140 }
  ];
const arr4 =   [
   { name: 'jhon', value: '50', height: 176, extraField: 1 },
   { name: 'Lisa', value: '25', height: 165 },
   { name: 'jack',  value: '12 ',  height: 190}
  ]; 
  
const getKey = (obj) => {
  return Object.keys(obj).map(k => `${k}:${obj[k]}`).join('');
};
 
const areEqual = (arr1, arr2) => {
  // First test length
  if(arr1.length !== arr2.length) return false;
  
  // Now lets grab a sets
  const names = new Set(arr1.concat(arr2).map((obj) => getKey(obj)));

  // now check Set length === arr1.length
  return (names.size * 2) === arr1.length + arr2.length;         
};

console.log(areEqual(arr1, arr2));
console.log(areEqual(arr1, arr3));
console.log(areEqual(arr1, arr4));
Bibberty
  • 4,670
  • 2
  • 8
  • 23
-1

A possible answer here could be to sort the array (base on some property in order to match perfectly in the second step) and stringify the array to compare.

function areArrayEqual(arrayA, arrayB) {
  if (arrayA.length !== arrayB.length) {
    return false;
  }

  return JSON.stringify(arrayA) === JSON.stringify(arrayB);
}
Tomeu Cabot
  • 433
  • 2
  • 8
  • My only comment here is that you cannot guarantee serialization order. It would be better to stringify each item into Set – Bibberty Mar 25 '21 at 00:07