-1

I have an array called "pedidos" which the length is x because it depends on the amount of "orders" we get this get's updated automatically through firebase.

each "order" has the following attributes:

acudiente: ""
banco: "Global Bank"
data: (12) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
email: ""
escuela: "Academia Internacional David"
estadoDeDisponibilidad: "Pendiente"
estadoDePago: "Pendiente"
fecha: "11/22/2021"
grado: "1° Grado"
id: "[AID]PM-345"
identificacion: ""
metodoDePago: "ACH"
nombre: "Brihana Orisiveth De Gracia Concepción "
phone: ""
total: "346.45"
totalPagado: 0
uid: "13eLyFqGGrZ0vTu3llpVxeFCBun2"

Now, each order has an attribute called "data" which is x because people can order x amount of stuff so 12 is not a constant. Each item inside data has the following attributes:

descripcion: "ACCELIUM - Programa de Tecnología"
disponible: 0
editorial: "Programa"
grado: (6) ['1° Grado', '2° Grado', '3° Grado', '4° Grado', '5° Grado', '6° Grado']
id: "L-25304"
materia: "Tecnologia"
ordenados: 12
precio: "35.00"
tipo: "Programa"

Now I'm trying to make a NEW Array that will contain every data array, which means that if there are 5 orders with 5 items each it should be an array of 25, this is a very simple for loop:

for(let x = 0; x < pedidos.length; x++){
      for(let y = 0; y < pedidos[x].data.length; y++){
        pedidosExcel.push(pedidos[x].data[y])
      }
    }

however I also want some attributes from the main array and not just the attributes from the array called "data", so I wanted to know if it was possible to MERGE attributes from each other.

Update

So I wasn't able to do what I wanted but I manage to get close to it with the following piece of code, however if I do this it ends up as an array inside an array for some reason. I manually set up every attribute because I wanted to overwrite some of them since some attributes have the same name

for(let x = 0; x < pedidos.length; x++){
      for(let y = 0; y < pedidos[x].data.length; y++){
        pedidosExcel.push(pedidos[x].data[y])
        pedidosExcel[y] = [{
          id: pedidos[x].id,
          nombre: pedidos[x].nombre,
          escuela: pedidos[x].escuela,
          grado: pedidos[x].grado,
          fecha: pedidos[x].fecha,
          descripcion: pedidos[x].data[y].descripcion,
          tipo: pedidos[x].data[y].tipo,
          editorial: pedidos[x].data[y].editorial,
          precio: pedidos[x].data[y].precio,
        }]
      }
    }

Hope this is more clear than my previous explanation.

ReactPotato
  • 1,262
  • 3
  • 26
  • 46
  • 1
    Does this answer your question? [How to duplicate object properties in another object?](https://stackoverflow.com/questions/9362716/how-to-duplicate-object-properties-in-another-object) – Dean Taylor Nov 23 '21 at 23:04
  • the merge option ? that didn't work sadly – ReactPotato Nov 23 '21 at 23:11
  • In the last image in your post, there are two keys with the name `id` - that is not possible, keys must be unique. Also, in the example above, the `data` array contains 12 items, so which item do you want merged into the parent data? – Ro Milton Nov 23 '21 at 23:27
  • Don't post images, post text containing the relevant information. It makes answering much easier as one can copy and paste and it is clearer to read. – pilchard Nov 23 '21 at 23:54
  • @RoMilton i'm sure they can be overwrite which is what I want to achieve. Alright so the main array contains a an array called data[x] that holds x items (in that picture is 12 but is not always the same). I made a new variable that will hold ALL the properties called data, so if in my collection of array I have 35 arrays that hold data[x] the new array should be the collection of all the information those data[x] holded so it would be 35 * data[x]. However I still need information from the first Array which is why I tried to overwrite it on my new update. – ReactPotato Nov 24 '21 at 00:02
  • @pilchard is just the console log I can copy and paste it if you want it ... but is not relevant for the code I'm not using that for the code – ReactPotato Nov 24 '21 at 00:03
  • I realize what it is, and the question is much more readable now you removed the images. Also it forces you to describe your issue, which can often lead to understanding it better rather than making the rest of us parse your logs – pilchard Nov 24 '21 at 00:18

2 Answers2

1

The problem is you're producing a nested array, but you want it to be flat. You have a couple of options:

Option 1: Use .flatMap():

This function is simply a combination of .map() and .flat()

const pedidos = [{
  id: '[AID]PM-345',
  nombre: '999999990',
  escuela: 'ABCDEF',
  grado: '1° grado ',
  fecha: '11/22/2021',
  data: [
    {
      descripcion: 'ACCELIUM - Programa',
      tipo: 'Programa',
      editorial: 'Programa',
      precio: '35.00'
    },
    {
      descripcion: 'ACCELIUM - Programa 2',
      tipo: 'Programa 2',
      editorial: 'Programa 2',
      precio: '45.00'
    }
  ]
}];

const pedidosExcel = pedidos.flatMap(pedido =>
  pedido.data.map(item => ({
    id: pedido.id,
    nombre: pedido.nombre,
    escuela: pedido.escuela,
    grado: pedido.grado,
    fecha: pedido.fecha,
    descripcion: item.descripcion,
    tipo: item.tipo,
    editorial: item.editorial,
    precio: item.precio
  }))
);

console.log(pedidosExcel);

Option 2 Use .reduce():

This function has the advantage of being used more widely than .flatMap() so may be more understandable. Maybe.

Reduce iterates through an array, and passes a value from one iteration to the next (acc in my example).

const pedidos = [{
  id: '[AID]PM-345',
  nombre: '999999990',
  escuela: 'ABCDEF',
  grado: '1° grado ',
  fecha: '11/22/2021',
  data: [
    {
      descripcion: 'ACCELIUM - Programa',
      tipo: 'Programa',
      editorial: 'Programa',
      precio: '35.00'
    },
    {
      descripcion: 'ACCELIUM - Programa 2',
      tipo: 'Programa 2',
      editorial: 'Programa 2',
      precio: '45.00'
    }
  ]
}];

const pedidosExcel = pedidos.reduce((acc, pedido) =>
  [
    ...acc,
    ...pedido.data.map(item => ({
      id: pedido.id,
      nombre: pedido.nombre,
      escuela: pedido.escuela,
      grado: pedido.grado,
      fecha: pedido.fecha,
      descripcion: item.descripcion,
      tipo: item.tipo,
      editorial: item.editorial,
      precio: item.precio
    }))
  ],
[]); 

console.log(pedidosExcel)

Use whichever you think is easiest and most understandable to your team.

Thank you for adding code to your question - next time please add the expected outcome and data as code as well.

Ro Milton
  • 2,281
  • 14
  • 9
  • Yeah this worked! thank you very much. I added the code but the console logs I screenshot them I didn't know people actually copy and pasted those. – ReactPotato Nov 24 '21 at 00:38
0

As far as I see you have an array with object inside it (not arrays), and inside this object you have many different keys, one of this keys is data, which is array with other objects. So what data model you want to get? Describe it in small example.

  • Alright I want to have Data in a different array (which is what I'm doing in the for loop) and I ALSO want to use "id" , "nombre", "colegio", "fecha" from the big first array and for that I need to merge them in some way in the for loop – ReactPotato Nov 23 '21 at 23:16
  • Your question is kinda tuff to understanding, especially with screenshots. Wrote some code (NOT SCREENSHOTS) to show data model that you want to have. 1. Simple model of data that you have 2. Simple model of data that you want to have –  Nov 23 '21 at 23:30
  • The code is there, the screenshots is just to show what data i'm moving around. The for loop pushes the array called data into a new variable call pedidosExcel grabing every value from the array called data of every array into the new variable. – ReactPotato Nov 23 '21 at 23:42
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 24 '21 at 01:22