Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

253
Visualizações
I know the object value, How can I return just the object property?

I'm doing this codewars I want return the object property is possible do it when I know the object value ?

function findOdd(A) {
    
        let counter = {};
        let finalResult;

   const NumInt =  A.sort((a, b)=> a - b)

    NumInt.forEach((e)=>{counter[e] = (counter[e] || 0) +1})
    console.log(counter)

    const values = Object.values(counter).filter((value)=>{
        return value % 2 !== 0
    })

    Object.values(counter).filter((value)=>{
       if(value % 2 !== 0){
           return console.log(value)
       }
    })
  }

  findOdd([1,1,2,-2,5,2,4,4,-1,-2,5])
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Using the Object.values() method returns an array of a given object's own enumerable property values so it's not what you want in your case.

You can iterate through the for...in statement which iterates over all enumerable properties of an object that are keyed by strings to achieve that.

function findOdd(A) {
    
        let counter = {};
        let finalResult;

   const NumInt =  A.sort((a, b)=> a - b)

    NumInt.forEach((e)=>{counter[e] = (counter[e] || 0) +1})
    console.log(counter)

    const values = Object.values(counter).filter((value)=>{
        return value % 2 !== 0
    })

    for (const el in counter){
       if(counter[el] % 2 !== 0){
           return console.log(el, counter[el]); //logs property name and it's value
       }
    }
  }

  findOdd([1,1,2,-2,5,2,4,4,-1,-2,5])

about 4 years ago · Juan Pablo Isaza Relatório

0

You can create an object storing the key,occurrences couples and then filter the only one key with an odd occurrence with reduce and Object#keys:

function findOdd(arr) {
  const occurrences = arr.reduce(function(acc, curr) {
    acc[curr] = acc[curr] ? acc[curr] + 1 : 1;
    return acc;
  }, {});
  const result = Object.keys(occurrences).find(key => (occurrences[key] % 2) === 1);
  
  return Number(result);
}

console.log(findOdd([7]) === 7);
console.log(findOdd([0]) === 0);
console.log(findOdd([1, 1, 2]) === 2);
console.log(findOdd([0, 1, 0, 1, 0]) === 0);
console.log(findOdd([1, 2, 2, 3, 3, 3, 4, 3, 3, 3, 2, 2, 1]) === 4);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can't loop an object using array built-in functions like map, forEach, for...of

I'm posting one solution here.

const data = {
   2: 0,
   1: 2,
   3: 1,
   4: 7,
   5: 9
}

for (const key in data) {
    if (data[key] % 2 === 1)
        console.log(key, data[key])
}

Other possible ways are using Object.keys() and Object.values()

Or Object.entries()

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda