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

159
Visualizações
Associate Array of Keys with Array of Values Javascript

I have one array of keys that I want to associate with all of the data returned from the API:

        const arr = [
          'cat',
          'dog',
          'horse',
        ]

const data_from_api = [
            ["cat_1",
            "dog_1",
            "horse_1"],

            ["cat_2",
            "dog_2",
            "horse_2"],
]

I want to create objects with the data that the keys area the values of this array and the values are the data that come from the API. How could I do this? Thanks in advance!

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

You could iterate over the api-data like this:

const arr = 
  ['cat',  'dog',  'horse',]
const data_from_api = [
  ['cat_1','dog_1','horse_1'],
  ['cat_2','dog_2','horse_2'],
]

const result = {}
data_from_api.forEach((api, i) =>

  // find array item (or create one, if not yet existant)
  (result[arr[i]] || (result[arr[i]] = []))

  // add current item
  .push(data_from_api[i])
)
console.log(result);

The result would be:

{
  cat: [["cat_1", "dog_1", "horse_1"]],
  dog: [["cat_2", "dog_2", "horse_2"]]
}
about 4 years ago · Santiago Gelvez 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