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

139
Visualizações
How do I convert an array object into a two-dimensional array

Here is a Array list.


const list = [
  {
    id: 5844,
    option: 'fruit'
    children: ['apple', 'banana', 'pear']
  },
  {
   id: 5845,
   option: 'vegetables'
   children: ['tomato', 'potato', 'spinach']
  }
]


I want to get a new array like this

apple of fruit's children is index 0

tomato of vegetables's children is index = 0

so they are match

[['apple', 'tomato'], ['banana', 'potato'], ['pear', 'spinach']]
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

I think we can try this piece of code

const list = [
  {
    id: 5844,
    option: 'fruit',
    children: ['apple', 'banana', 'pear']
  },
  {
    id: 5845,
    option: 'vegetables',
    children: ['tomato', 'potato', 'spinach']
  }
]

var ans = []
list.forEach(item => {

  item.children.forEach((child, index) => {

    if (!ans[index]) {
      ans[index] = []
      ans[index].push(child)
    } else {

      ans[index].push(child)
    }
  })

})
about 4 years ago · Juan Pablo Isaza Relatório

0

With this solution it doesn't matter how many objects are in the array. You can map over the children in the first object and use it's length to return a flatMap of the children elements.

const list=[{id:5844,option:"fruit",children:["apple","banana","pear"]},{id:5845,option:"vegetables",children:["tomato","potato","spinach"]},{id:5846,option:"buildings",children:["church","warehouse","skyscraper"]}];

function getNewData(list) {

  // `map` over the children in the first object
  // using its index to return a new flattened array
  // of all array object children
  return list[0].children.map((_, i) => {
    return list.flatMap(obj => obj.children[i]);
  });
}

console.log(getNewData(list));

about 4 years ago · Juan Pablo Isaza Relatório

0

I assume your children have same length. We can use 2 loop to group the element of children.

First loop to iterate the element of children.

Second loop to iterate the element of list.

Here is simple code to solve your case.

var listSize = list.length;
var childSize = list[0].children.length;
var expectedArrs = [];
for(var i=0;i<childSize;i++){
  var groupByChild = [];
  for(var j=0;j<listSize;j++){
     groupByChild.push(list[j].children[i]);
  }
  expectedArrs.push(groupByChild);
}
console.log(expectedArrs);

The result of console:

[["apple", "tomato"], ["banana", "potato"], ["pear", "spinach"]]
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