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

127
Visualizações
JS Convert Object with Properties as Array into Array List of Objects

I have the following data incoming (reduced the objects for easier comprehension). My properties could include hundred to thousands of properties.

teams = {
  teams: ['Lakers', 'Clippers', 'Bucks', 'Suns'],
  states: ['California', 'California', 'Wisconsin', 'Arizona']
}

Note that the properties will always contain the same number of items for each.

I'd like to convert it to an Array List of objects:

teamsList = [
  { teams: 'Lakers', states: 'California' },
  { teams: 'Clippers', states: 'California' },
  { teams: 'Bucks', states: 'Wisconsin' },
  { teams: 'Suns', states: 'Arizona' }
]

This is what I have initially in mind and tried:

const parseData = (obj) => {
  const teams = obj.teams;
  const states = obj.states;
  let newArrayList = [];
  teams.forEach((item, index) => {
    const teamData = {
      teams: item,
      states: states[index]
    }
    newArrayList.push(teamData);
  })

  return newArrayList;
}

I am wondering if there is a more optimal way to do this?

Forgot to add that I am looking for ways so that each object property in the new array list should be dynamic as well, like determine the keys for the new object without writing each specific property:

  teams.forEach((item, index) => {
    const teamData = {
      teams: item,
      states: states[index],
      n: n[index]
    }
    newArrayList.push(teamData);
  })

Where n corresponds to any number of possible object keys.

Thanks.

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

0

Map one of the arrays, and use the index to find the matching value in the other array, and return an object.

const teams = {
  teams: ['Lakers', 'Clippers', 'Bucks', 'Suns'],
  states: ['California', 'California', 'Wisconsin', 'Arizona']
};
const teamList = teams.teams.map((t, i) => ({ teams: t, states: teams.states[i] }));
console.log(teamList);

If you have lots of properties, iterate over the entries.

const teams = {
  teams: ['Lakers', 'Clippers', 'Bucks', 'Suns'],
  states: ['California', 'California', 'Wisconsin', 'Arizona']
};
const entries = Object.entries(teams);
const [key, arr] = entries.shift();
const teamList = arr.map((val, i) => Object.fromEntries(
    [[key, val]].concat(
      entries.map(entry => [entry[0], entry[1][i]])
    )
));
console.log(teamList);

about 4 years ago · Juan Pablo Isaza Relatório

0

This solution works by first determining the length of the longest array, and then creating new objects dynamically based on the keys available in your data:

const data = {
  teams: ['Lakers', 'Clippers', 'Bucks', 'Suns'],
  states: ['California', 'California', 'Wisconsin', 'Arizona']
};

const result = [
  ...Array(Object.values(data).reduce((a, {length}) => Math.max(a, length), 0))
].map((_, i) => Object.keys(data).reduce((a, k) => ({...a, [k]: data[k][i]}), {}));

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

Use Array.map() which will iterate each element and return final array of object.

teams = {
  teams: ['Lakers', 'Clippers', 'Bucks', 'Suns'],
  states: ['California', 'California', 'Wisconsin', 'Arizona']
};

const parseData = (obj) => {
  const states = teams.states;
  return obj.teams.map((teams, index) => ({ teams, states: states[index] }));
}

console.log(parseData(teams));

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