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

581
Visualizações
How to iterate an array of objects

I need to go through an array of objects and return an array that contains the "taxNumber" ordered by the names. How could I do it?

const paddockManagers = 
  [ { id: 1, taxNumber: '132254524', name: 'JUAN TAPIA BURGOS'         } 
  , { id: 2, taxNumber: '143618668', name: 'EFRAIN SOTO VERA'          } 
  , { id: 3, taxNumber: '78903228',  name: 'CARLOS PEREZ GONZALEZ'     } 
  , { id: 4, taxNumber: '176812737', name: 'ANDRES VIÑALES CIENFUEGOS' } 
  , { id: 5, taxNumber: '216352696', name: 'OSCAR PEREZ ZUÑIGA'        } 
  , { id: 6, taxNumber: '78684747',  name: 'JOAQUIN ANDRADE SANDOVAL'  } 
  ] 

function listPaddockManagersByName() {
  return paddockManagers.map((paddockManager) =>
    addockManager.name + paddockManager.taxNumber)
};
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Use Array.sort and String.localCompare to sort the array lexographically (by the name property), then map over the result to get the taxNumber property.

const paddockManagers = [

{ id: 1, taxNumber: '132254524', name: 'JUAN TAPIA BURGOS'},

{ id: 2, taxNumber: '143618668', name: 'EFRAIN SOTO VERA'},

{ id: 3, taxNumber: '78903228', name: 'CARLOS PEREZ GONZALEZ'},

{ id: 4, taxNumber: '176812737', name: 'ANDRES VIÑALES CIENFUEGOS'},

{ id: 5, taxNumber: '216352696', name: 'OSCAR PEREZ ZUÑIGA'},

{ id: 6, taxNumber: '78684747', name: 'JOAQUIN ANDRADE SANDOVAL' }
];

const result = paddockManagers.sort((a, b) => a.name.localeCompare(b.name)).map(e => e.taxNumber)
console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

sort method will change your array initial order.
If you want to preserve this initial order, you need to duplicate your array first ( .map() )

const paddockManagers = 
  [ { id: 1, taxNumber: '132254524', name: 'JUAN TAPIA BURGOS'         } 
  , { id: 2, taxNumber: '143618668', name: 'EFRAIN SOTO VERA'          } 
  , { id: 3, taxNumber: '78903228',  name: 'CARLOS PEREZ GONZALEZ'     } 
  , { id: 4, taxNumber: '176812737', name: 'ANDRES VIÑALES CIENFUEGOS' } 
  , { id: 5, taxNumber: '216352696', name: 'OSCAR PEREZ ZUÑIGA'        } 
  , { id: 6, taxNumber: '78684747',  name: 'JOAQUIN ANDRADE SANDOVAL'  } 
  ] 

const listPaddockManagersByName = arr =>
   arr
    .map(row=>row)
    .sort((a,b)=>a.name.localeCompare(b.name))
    .map(({name,taxNumber}) => `${name}, ${taxNumber}`)

let result = listPaddockManagersByName( paddockManagers )

console.log( paddockManagers )
console.log( result )
.as-console-wrapper { max-height: 100% !important; top: 0 }

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