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

81
Visualizações
Trying to make array with certain length

I'm trying to create an array from two other arrays using a loop, where in both arrays I compare two strings and when I find two strings that are the same I push the value to the array from second compared array, and if I don't find it I push 0. The problem is that the array has to be exactly the length of the first array.

const array1 = ['21', '22', '23', '24', '25', '26', '27']
const array2 = [{score: 1, date: '21'}, {score: 3, date: '23'} ] 

output should look like this: [1, 0, 3, 0, 0, 0, 0] <=== this array must have length of array1, and i have problem to create proper loop, or maybe this is shouldnt be loop?

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

0

You can achieve this by combining map() (to iterate over the first array) and find() (to get the right score out of the second array). To fall back to 0 in case there is no result in array2, you can combine optional chaining with nullish coalescing.

const array1 = ['21', '22', '23', '24', '25', '26', '27'];
const array2 = [{score: 1, date: '21'}, {score: 3, date: '23'} ];

const array3 = array1.map(value => array2.find(obj => obj.date === value)?.score ?? 0);
console.log(array3);

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