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

328
Vistas
Is there a way to return a conditional object value and key?

I am new and learning programming. I wondered if there is a way to get a specific value + key or (in this case) key from an object if it passes a condition.

function miti (a){
    let obj = {};
    let num;
    for(let i =0; i < a.length; i++){
        num = a[i]
        if(obj[num] === undefined){
          obj[num]= 1
        } else {
          obj[num] ++
        }
   }
   //Now I have created an object that records the frequancy of each presented number.
   if(Object.values(obj) === 1){
     return 
   }
}
console.log(miti([1,2,1,3,4,3,4,5))

From the above code, I would like to extract a lonely number with no pairs, I built an object that records each frequency from a given array.

Please be a little descriptive since I am a newbie.

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

0

Object.values(obj) === 1 doesn't make any sense because Object.values returns an array, which definitely won't be equal to a number.

Iterate through the entries of the object (the key-value pairs) and return the first entry for which the value is 1.

function miti(a) {
  let obj = {};
  let num;
  for (let i = 0; i < a.length; i++) {
    num = a[i]
    if (obj[num] === undefined) {
      obj[num] = 1
    } else {
      obj[num]++
    }
  }
  for (const entry of Object.entries(obj)) {
    if (entry[1] === 1) {
      return entry[0];
      // or return entry, if you want the whole entry and not just the key
    }
  }
}
console.log(miti([1, 2, 1, 3, 4, 3, 4, 5]))

Or .find the entry matching the condition and return it.

function miti(a) {
  let obj = {};
  let num;
  for (let i = 0; i < a.length; i++) {
    num = a[i]
    if (obj[num] === undefined) {
      obj[num] = 1
    } else {
      obj[num]++
    }
  }
  return Object.entries(obj)
    .find(entry => entry[1] === 1)
    [0];
}
console.log(miti([1, 2, 1, 3, 4, 3, 4, 5]))

about 4 years ago · Juan Pablo Isaza Denunciar

0

Or, using the Array methods .forEach() and .find() in combination with Object.keys() you can do it like that:

function firstUniqueValue(a) {
  let obj = {};
  a.forEach(k=>obj[k]=(obj[k]||0)+1);
  return Object.keys(obj).find(k=>obj[k]==1);
}
console.log(firstUniqueValue([1, 2, 1, 3, 4, 3, 4, 5]))

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