Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!