1

i was wondering if there is quick solution (using map() for example) to convert a multidimensional array to a single dimensional array, that still keeps all information of the original array.

You can solve it with some loops, but i think there must be a smarter solution.

To explain it a bit more here is an example:

const arr = [{
  id: 1,
  people: [
    {name: "x", surname: "x"},
    {name: "y", surname: "y"}
  ]
 },{
  id: 2,
  people: [
    {name: "a", surname: "a"},
    {name: "b", surname: "b"}
  ]
 }]

const result = [
 {id: 1, name: "x", surname: "x"},
 {id: 1, name: "y", surname: "y"},
 {id: 2, name: "a", surname: "a"},
 {id: 2, name: "b", surname: "b"}
]

3 Answers 3

2

Use Array.flatMap() with an internal Array.map() to add the id:

const arr = [{"id":1,"people":[{"name":"x","surname":"x"},{"name":"y","surname":"y"}]},{"id":2,"people":[{"name":"a","surname":"a"},{"name":"b","surname":"b"}]}]
 
const result = arr.flatMap(({ id, people }) => people.map(p => ({ id, ...p })))

console.log(result)

If Array.flatMap() is not supported by your target browsers, use Array.map(), and spread into Array.concat():

const arr = [{"id":1,"people":[{"name":"x","surname":"x"},{"name":"y","surname":"y"}]},{"id":2,"people":[{"name":"a","surname":"a"},{"name":"b","surname":"b"}]}]
 
const result = [].concat(...arr.map(({ id, people }) => people.map(p => ({ id, ...p }))))

console.log(result)

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

2 Comments

Is there a smart way to add the id to every object in the array? flatMap() does the right think but the id field is lost.
Missed that. You just need to map the people as well, and add the id. See update.
1

You can use array.reduce to create multiple entries for each people, assuming the array never changes its form. Further comments about how it works are directly in the code.

const arr = [{
  id: 1,
  people: [
    {name: "x", surname: "x"},
    {name: "y", surname: "y"}
  ]
 },{
  id: 2,
  people: [
    {name: "a", surname: "a"},
    {name: "b", surname: "b"}
  ]
 }]

// Reduce the origianal array.
const r = arr.reduce((acc, {id, people}) => {
  // For each people, push a new elemen to the array.
  return people.forEach(_people => {
    // the element pushed will hold the [id] and all the properties of the currently looped [people] item.
    acc.push(Object.assign({id}, _people));
  }), acc; // <-- finally, return the accumulator for the next iteration.
}, []); // <-- provide a new empty array as an initial value.
console.log(r);

Comments

1

You can try with map() and flat()

const arr = [{
  id: 1,
  people: [
    {name: "x", surname: "x"},
    {name: "y", surname: "y"}
  ]
 },{
  id: 2,
  people: [
    {name: "a", surname: "a"},
    {name: "b", surname: "b"}
  ]
 }]

const result = arr.map(p => {
  return p.people.map(pi => Object.assign(pi, {id: p.id}));
}).flat();
console.log(result);

Comments

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.