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

121
Visualizações
unexpected result while using findIndex array method

Below is a code I am using

  const account1 = {
      owner: 'Jonas Schmedtmann',
      movements: [200, 450, -400, 3000, -650, -130, 70, 1300],
      interestRate: 1.2, // %
      pin: 1111,
    };
    
    const account2 = {
      owner: 'Jessica Davis',
      movements: [5000, 3400, -150, -790, -3210, -1000, 8500, -30],
      interestRate: 1.5,
      pin: 2222,
    };
    
    const account3 = {
      owner: 'Steven Thomas Williams',
      movements: [200, -200, 340, -300, -20, 50, 400, -460],
      interestRate: 0.7,
      pin: 3333,
    };
    const account4 = {
      owner: 'Sarah Smith',
      movements: [430, 1000, 700, 50, 90],
      interestRate: 1,
      pin: 4444,
    };
    const accounts = [account1, account2, account3, account4];
    const index = accounts.findIndex(function (acc) {
      acc.pin === 2222;
    });
    console.log(index);

The expected result is 1 but why am I getting -1. I get -1 for whatever condition I put on the findIndex method.

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

0

You only need to make the return inside the findIndex function (to return the index where the condition is true). When findIndex no match with some result returns -1.

    const account1 = {
      owner: 'Jonas Schmedtmann',
      movements: [200, 450, -400, 3000, -650, -130, 70, 1300],
      interestRate: 1.2, // %
      pin: 1111,
    };
    
    const account2 = {
      owner: 'Jessica Davis',
      movements: [5000, 3400, -150, -790, -3210, -1000, 8500, -30],
      interestRate: 1.5,
      pin: 2222,
    };
    
    const account3 = {
      owner: 'Steven Thomas Williams',
      movements: [200, -200, 340, -300, -20, 50, 400, -460],
      interestRate: 0.7,
      pin: 3333,
    };
    const account4 = {
      owner: 'Sarah Smith',
      movements: [430, 1000, 700, 50, 90],
      interestRate: 1,
      pin: 4444,
    };
    const accounts = [account1, account2, account3, account4];
    const index = accounts.findIndex(function (acc) {
      return acc.pin === 2222;
    });
    console.log(index);

And I prefer the code like this:

    const accounts = [
        {
          owner: 'Jonas Schmedtmann',
          movements: [200, 450, -400, 3000, -650, -130, 70, 1300],
          interestRate: 1.2, // %
          pin: 1111,
        },
        {
          owner: 'Jessica Davis',
          movements: [5000, 3400, -150, -790, -3210, -1000, 8500, -30],
          interestRate: 1.5,
          pin: 2222,
        },
        {
          owner: 'Steven Thomas Williams',
          movements: [200, -200, 340, -300, -20, 50, 400, -460],
          interestRate: 0.7,
          pin: 3333,
        },
        {
          owner: 'Sarah Smith',
          movements: [430, 1000, 700, 50, 90],
          interestRate: 1,
          pin: 4444,
        }
    ];

    const index = accounts.findIndex(function (acc) {
      return acc.pin === 2222;
    });

    console.log(index);
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