0

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']]
3
  • 1
    Can you add the code you've attempted to your question as a minimal reproducible example. Commented Oct 23, 2021 at 7:17
  • Also, are you always only going to have two objects whose children you iterate over? Commented Oct 23, 2021 at 7:28
  • no, it may be many objects Commented Oct 23, 2021 at 7:34

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

what a beautiful code! Thank you ! But i'm not very understand this code, so i am seeing mdn
map returns an array by looping over and array. All the code does is use the first object as a guide to return a new array of items from all of the children array which it then flattens. You could easily have used forEach or a nested loop but I this approach. Happy coding!
I understand , that's so clean code.
PS if you log bits of the code to the browser console you see see how it works. It's a useful debugging tool.
1

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

})

Comments

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

Comments

0

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

1 Comment

But, it not only two object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.