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

207
Vistas
how to condition inequality of object properties

I need condition to be true only if all properties of the object (variables) are not equal to each other. How can I condition object properties' inequality in a more optimal way? If the number of them grows the combination of obj[1]!=obj[2] and so on will grow enormously

var obj = {
                1: linksArray[randomNumber],
                2: linksArray[randomNumber2],
                3: linksArray[randomNumber3]
            } 
                    
            if(obj[1]!=obj[2] && obj[1]!=obj[3] &&
                obj[2]!=obj[3] && obj[2]!=obj[1] &&
                obj[3]!=obj[2] && obj[3]!=obj[1]
                ) {
                    arr.push(linksArray[randomNumber]);
                    arr.push(linksArray[randomNumber2]);
                    arr.push(linksArray[randomNumber3]);
                    break;
                }  
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Get the object's values, and create a Set. If the Set's size is identical to the values length, all values are unique:

const allUnique = obj => {
  const values = Object.values(obj)
  
  return new Set(values).size === values.length
}

const obj1 = { 1: 'a', '2': 'b', 3: 'c' }
const obj2 = { 1: 'a', '2': 'b', 3: 'c', 4: 'b' }

console.log(allUnique(obj1)) // true
console.log(allUnique(obj2)) // false

about 4 years ago · Juan Pablo Isaza Denunciar

0

Put all the values in an array, then use it to build a Set (data structure where all items are different, documentation).

If the array has the same amount of items as the set, you're sure they're all different.

var obj1 = {
  field1: true,
  fiedl2: 2,
  field3: "red",
}

var obj2 = {
  field1: true,
  fiedl2: 2,
  field3: "red",
  field4: 2
}

function allDifferent(obj) {
  const values = Object.values(obj);
  return new Set(values).size === values.length
}

console.log(allDifferent(obj1));
console.log(allDifferent(obj2));

about 4 years ago · Juan Pablo Isaza Denunciar

0

Check for duplicates by making a Set of all the object values - these will be unique. Then compare the size of the Set to the number of key/values in the original object. If they are different - there is a duplicate in the original. If they are the same, the values of the original are unique.

const uniqueValues = new Set(Object.keys(obj));
if (uniqueValues.size == obj.length) {...do stuff}
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