Home > Back-end >  How do I convert an array object into a two-dimensional array
How do I convert an array object into a two-dimensional array

Time:10-23

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']]

CodePudding user response:

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));
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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"]]

CodePudding user response:

It is resolved such that:

const result = []
for(let i = 0; i< List[0].children; i  ){
const result1 = []
result1.push(list[0].children[i] )
result1.push(list[1].children[i])

result.push(result1)
}

CodePudding user response:

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)
    }
  })

})

CodePudding user response:

  var list = [
  {
    id: 5844,
    option: 'fruit',
    children: ['apple', 'banana', 'pear']
  },
  {
   id: 5845,
   option: 'vegetables',
   children: ['tomato', 'potato', 'spinach']
  }
]
    
  var newArr = [];
  var i=0;
while(i<list.length){
    newArr.push(list[i].children);
    i=i 1;
}

console.log(newArr)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related