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

423
Visualizações
How can I skip to value to specific key in JSON object?

So I am dealing with an object that looks a bit like this:

{
  examples: [
    { key: 'value1' },
    { key: 'value1' },
    { key: 'value1' },
    { key: 'value2' },
    { key: 'value2' },
    { key: 'value2' }
  ]
}

As you can see the values are ordered. I'm trying to get where the value is value2 and I'm sure there is a more efficient way than what I am doing currently, as the actual object is significantly larger and so it takes some time to reach value2. This is the function I created:

function getValue2 (obj, num) {
    if (obj.examples[num] = "value2"){
        console.log(obj.examples[num]);
    }
    else {
        getValue2(obj, num + 1);
    };
};

var x = JSON.parse(obj);
getValue2(x, 0);

Thank you in advance! :)

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can use Array#find:

returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

const data = {
  examples: [ { key: 'value1' }, { key: 'value1' }, { key: 'value1' }, { key: 'value2' }, { key: 'value2' }, { key: 'value2' } ]
};

const res = data.examples.find(({ key }) => key === 'value2');

console.log(res);

To get the index of the first occurrence, you can use Array#findIndex:

returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.

const data = {
  examples: [ { key: 'value1' }, { key: 'value1' }, { key: 'value1' }, { key: 'value2' }, { key: 'value2' }, { key: 'value2' } ]
};

const index = data.examples.findIndex(({ key }) => key === 'value2');

console.log(index);

about 4 years ago · Juan Pablo Isaza Relatório

0

How about using Object.values, like this:

function getValue2(obj){
   return Object.values(obj.examples).find(e => e === 'value2')
} 
about 4 years ago · Juan Pablo Isaza Relatório

0

The first problem I see is you are using a single equals = to check equality when it should be == or ===.

As for alternative ways to run a similar function, I suggest you look up and learn about the for and while loops, as they are essential for JS.

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