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

113
Visualizações
Making an object from two arrays

I have two arrays of objects:

const headers = [
  {
    ID: "1",
    Header: "General",
  },
  {
    ID: "2",
    Header: "Misc",
  },
  ...
]

const terms = [
  {
    Header: "General",
    Term: "Lorem ipsum",
    Definition: "Lorem ipsum",
  },      
  {
    Header: "General",
    Term: "Lorem ipsum",
    Definition: "Lorem ipsum",
  },
  {
    Header: "Misc",
    Term: "Lorem ipsum",
    Definition: "Lorem ipsum",
  },
  {
    Header: "General",
    Term: "Lorem ipsum",
    Definition: "Lorem ipsum",
  },
  {
    Header: "General",
    Term: "Lorem ipsum",
    Definition: "Lorem ipsum",
  },
  {
    Header: "General",
    Term: "Lorem ipsum",
    Definition: "Lorem ipsum",
  },
  ...
]

And I would like to have one object that has each header as a key and with an array of all the terms as the values:

{
  "General: [
    {
      Term: "Lorem ipsum",
      Definition: "Lorem ipsum",
    }
    {
      Term: "Lorem ipsum",
      Definition: "Lorem ipsum",
    },
    ...
  ],
  "Misc": [
    {
      Term: "Lorem ipsum",
      Definition: "Lorem ipsum",
    },
    {
      Term: "Lorem ipsum",
      Definition: "Lorem ipsum",
    },
    {
      Term: "Lorem ipsum",
      Definition: "Lorem ipsum",
    },
    ...
  ],
  ...
}

I'm guessing that there is a way to do it with .map .foreach .filter or some combination, but have been hitting a wall all day. Including just trying to put the problem into words of what I am trying to do here so I can find better search results. Any help would appreciated. Thanks!

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

0

A basic way to do it if you're not needing additional properties would be:

const outputObj = {}
headers.forEach(header => {
    const headerArr = outputObj[header.Header] = [];
    terms.forEach(term => {
        const {Header, Term, Definition} = term;
        if (Header === header.Header) {
            headerArr.push({Term, Definition});
        }
    })
})

This would loop through each header and add the Header property as a key with an empty array, then the second loop would go through the terms and add just the Term and Definition (omitting the Header).

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use a combination of reduce and forEach to get your result. This would only have to loop through your arrays once, instead of looping through terms twice, as would happen with nested forEach loops.

const obj = headers.reduce((a, o) => { a[o.Header] = []; return a; }, {});
terms.forEach(({ Header, ...x }) => obj[Header].push(x));

Explanation: The reduce converts from an array to an object. Then the forEach loops through and adds the terms to the newly created object.

Here's a working example:

const headers = [
  {
    ID: '1',
    Header: 'General',
  },
  {
    ID: '2',
    Header: 'Misc',
  },
];

const terms = [
  {
    Header: 'General',
    Term: 'Lorem ipsum',
    Definition: 'Lorem ipsum',
  },
  {
    Header: 'General',
    Term: 'Lorem ipsum',
    Definition: 'Lorem ipsum',
  },
  {
    Header: 'Misc',
    Term: 'Lorem ipsum',
    Definition: 'Lorem ipsum',
  },
  {
    Header: 'General',
    Term: 'Lorem ipsum',
    Definition: 'Lorem ipsum',
  },
  {
    Header: 'General',
    Term: 'Lorem ipsum',
    Definition: 'Lorem ipsum',
  },
  {
    Header: 'General',
    Term: 'Lorem ipsum',
    Definition: 'Lorem ipsum',
  },
];

const obj = headers.reduce((a, o) => { a[o.Header] = []; return a; }, {});
terms.forEach(({ Header, ...x }) => obj[Header].push(x));
console.log(obj);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can make use of forEach here as:

const dict = {};
headers.forEach(({ Header }) => (dict[Header] = []));
terms.forEach(({ Header, ...rest }) => dict[Header].push(rest));
console.log(dict);

const headers = [
  {
    ID: "1",
    Header: "General",
  },
  {
    ID: "2",
    Header: "Misc",
  },
];

const terms = [
  {
    Header: "General",
    Term: "Lorem ipsum",
    Definition: "Lorem ipsum",
  },
  {
    Header: "General",
    Term: "Lorem ipsum",
    Definition: "Lorem ipsum",
  },
  {
    Header: "Misc",
    Term: "Lorem ipsum",
    Definition: "Lorem ipsum",
  },
  {
    Header: "General",
    Term: "Lorem ipsum",
    Definition: "Lorem ipsum",
  },
  {
    Header: "General",
    Term: "Lorem ipsum",
    Definition: "Lorem ipsum",
  },
  {
    Header: "General",
    Term: "Lorem ipsum",
    Definition: "Lorem ipsum",
  },
];

const dict = {};
headers.forEach(({ Header }) => (dict[Header] = []));
terms.forEach(({ Header, ...rest }) => dict[Header].push(rest));
console.log(dict);

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