duplicates in an array return, where I need to save the entire array structure in mongodb

Viewed 35

how could I avoid a duplicate in array, I'm looping the array I receive, and I need to save in mongodb, so it's duplicating the array's indexes, maybe using a filter I thought, wouldn't it be harmful to the system maybe?, and how could in this case use a filter to filter duplicates in an array with a structure that has objects, strings, etcs

const hasDate = await Universities.find({country})
  if(hasDate.length > 0) {
    return new Error('Dados já existe');
  } else {
    for(let i in returnApi) {
      await Universities.create({
        domains: returnApi[i].domains,
        alpha_two_code: returnApi[i].alpha_two_code,
        country: returnApi[i].country,
        web_pages: returnApi[i].web_pages,
        name: returnApi[i].name,
        state_province: returnApi[i].stateprovince
      })
    };
    await insertNameUniversities(returnApi)
  };
};
1 Answers

Ok so basically we can use some deep compare method we found online.

const person1 = {
    "firstName": "John",
    "lastName": "Doe",
    "age": 35 
};

const person2 = {
    "firstName": "John",
    "lastName": "Doe",
    "age": 35,
};
 
const isDeepEqual = (object1, object2) => {

  const objKeys1 = Object.keys(object1);
  const objKeys2 = Object.keys(object2);

  if (objKeys1.length !== objKeys2.length) return false;

  for (var key of objKeys1) {
    const value1 = object1[key];
    const value2 = object2[key];

    const isObjects = isObject(value1) && isObject(value2);

    if ((isObjects && !isDeepEqual(value1, value2)) ||
      (!isObjects && value1 !== value2)
    ) {
      return false;
    }
  }
  return true;
};

const isObject = (object) => {
  return object != null && typeof object === "object";
};

console.log(isDeepEqual(person1, person2)); //true

As for the duplicates removal, let's do it manually, just 2 nested loops

const person1 = {
  "firstName": "John",
  "lastName": "Doe",
  "age": 35
};

const person2 = {
  "firstName": "Frank",
  "lastName": "Fabian",
  "age": 72,
};


var arr = [person1, person2, { ...person1 }]

// console.log(arr)
const isDeepEqual = (object1, object2) => {

  const isObject = (object) => {
    return object != null && typeof object === "object";
  };

  const objKeys1 = Object.keys(object1);
  const objKeys2 = Object.keys(object2);

  if (objKeys1.length !== objKeys2.length) return false;

  for (var key of objKeys1) {
    const value1 = object1[key];
    const value2 = object2[key];

    const isObjects = isObject(value1) && isObject(value2);

    if ((isObjects && !isDeepEqual(value1, value2)) ||
      (!isObjects && value1 !== value2)
    ) {
      return false;
    }
  }
  return true;
};


function removeDuplicates(arr) {
  for (var i = 0; i < arr.length - 1; i++) {
    for (var j = i + 1; j < arr.length; j++) {
      if (isDeepEqual(arr[i], arr[j])) {
        arr[j] = 'delete-me-later'
      }
    }
  }
  return arr.filter(item => item !== 'delete-me-later');
}


console.log(removeDuplicates(arr))
.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}

Related