Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

109
Vistas
check if array object does not contain a object

I just started to learn javascript today and i'm trying to check an array object to check if the array does NOT contain a object.

For an example i am trying to check if data2 does not contain data1.

let data1 = [{ x: 6232, y: 10536, type: "data4", dead: 0, uid: 3832864 }];

let data2 = [
  { x: 6352, y: 10656, type: "data1", dead: 0, uid: 3832861 },
  { x: 6322, y: 10546, type: "data2", dead: 0, uid: 3832862 },
  { x: 6542, y: 15356, type: "data3", dead: 0, uid: 3832863 }
];

let check = Object.entries(data2).find(([key, value]) => value.uid != data1[0].uid);
console.log(check); // Expected output to be fales.

If someone could help me or point me in the right direction i would really appreciate it.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You need to compare (===) the result of find with !undefined.

Edit: You do not need to call entries on an array.

const data1 = [{ x: 6232, y: 10536, type: "data4", dead: 0, uid: 3832864 }];

const data2 = [
  { x: 6352, y: 10656, type: "data1", dead: 0, uid: 3832861 },
  { x: 6322, y: 10546, type: "data2", dead: 0, uid: 3832862 },
  { x: 6542, y: 15356, type: "data3", dead: 0, uid: 3832863 }
];

const check = data2.find(value => value.uid === data1[0].uid) !== undefined;

console.log(check); // false

As others have noted, you can short-circuit with some. Here is an optimized version:

/**
 * Detrmines if an object exists within an array of object
 * @param {Object[]} arr - An array of object to search
 * @param {Object} obj - An object to search for
 * @param {Function} fn - A pluck function used for comparing
 * @return {Boolean} Returns true if the item exists
 */
const contains = (arr, obj, fn) =>
  (res => arr.some(e => fn(e) === res)) // Compare 
  (fn(obj)); // Pre-calculate as `res`
  
const data1 = [{ x: 6232, y: 10536, type: "data4", dead: 0, uid: 3832864 }];

const data2 = [
  { x: 6352, y: 10656, type: "data1", dead: 0, uid: 3832861 },
  { x: 6322, y: 10546, type: "data2", dead: 0, uid: 3832862 },
  { x: 6542, y: 15356, type: "data3", dead: 0, uid: 3832863 }
];

const [obj] = data1; // Destructure first item

console.log(contains(data2, obj, ({ uid }) => uid)); // false

console.log(contains(data2, { uid: 3832862 }, ({ uid }) => uid)); // true

about 4 years ago · Juan Pablo Isaza Denunciar

0

The way you are tackling this problem is okay, and the other answer will help you fix it. But this is ideally supposed to be solved as an array. With Object.entries() you are getting the array objects and indices as values & keys respectively. Then you iterate over them.

You can notice you are not even using the key variable.

No need to do all this since you already have an array.

You can use .some(), which returns true/false based on a condition which even one of the array item fulfills. Exactly what you need.

let data1 = [{ x: 6232, y: 10536, type: "data4", dead: 0, uid: 3832864 }];

let data2 = [
  { x: 6352, y: 10656, type: "data1", dead: 0, uid: 3832861 },
  { x: 6322, y: 10546, type: "data2", dead: 0, uid: 3832862 },
  { x: 6542, y: 15356, type: "data3", dead: 0, uid: 3832863 }
];

let check = data2.some(x => x.uid === data1[0].uid);
console.log(check); // Expected output to be fales.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You have a few syntax errors in your code above. data2 is an array of objects so you don't need to do Object.entries() to get the elements of the array. I would use Array.some() method here so that it returns a boolean value instead of the actual data element if you are hoping to have a true or false value logged. the Array.some() method just returns once one case is true and we don't have to go through all the entries in the array of object if we already found a match.

let data1 = [{ x: 6232, y: 10536, type: "data4", dead: 0, uid: 3832864 }];

let data2 = [
  { x: 6352, y: 10656, type: "data1", dead: 0, uid: 3832861 },
  { x: 6322, y: 10546, type: "data2", dead: 0, uid: 3832862 },
  { x: 6542, y: 15356, type: "data3", dead: 0, uid: 3832863 },
  { x: 6232, y: 10536, type: "data4", dead: 0, uid: 3832864 }
];

let check = data2.some(entry => entry.uid == data1[0].uid);
console.log(check); // Expected output to be true.

If you didn't have a unique id for each entry then a quick and dirty way to do this would be to stringify both objects and then compare the strings. This has its limitations because the order of the fields in your object must be the same.

var object = { 'a': 1, 'b':2};
var other = { 'a': 1, 'b':2 };
 
console.log(JSON.stringify(object) == JSON.stringify(other)); //this will print true

For deep object comparison, I like to use the isequal method from lodash

var object = { 'a': 1, 'b':2};
var other = { 'b':2, 'a': 1 };
 
console.log(JSON.stringify(object) == JSON.stringify(other)); //this will print false
console.log(_.isEqual(object, other)); //this will print true

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda